Android TextView Define position of forced newline - java

In my application, Im having a Textview containing text in this style:
"123131 (Ø 374)"
On small devices, the text reaches the viewborder causing a new line at one of these spaces. In some cases, this makes the text look like this:
"123131 (Ø
374)"
Is there a way to force the new line to the first whitespace without using 2 TextViews? Isnt there some kind of "protected white space" or something like this?
"123131
(Ø 374)"
regards
danijoo

I solved it by recplacing all whitespaces but one with the unicode code for a nonbreaking whitespace:
"123131 (Ø\u00a0374)"

What I'd do is:
Implement a custom TextView with a listener when Ellipsis is triggered:
http://thanksmister.com/2012/10/16/android-textview-with-ellipsis-listener/
Then, when receiveing the listener, programmatically adding a '\n' character right before the '(' character.
Remember to set that TextView to be multi line.

Related

How to add specific character after at every 10 character on edittext

I have a edittext on which i want to add special character "!" after every 10 characters.
So for example if i write :
9090909090!164960375830!0583948
And when i remove text from then it will auto remove "!".
I have tried addTextChangedListener but wont able to achieved it.
How can i get it ?

EditText, use textMultiline and Single Line at the same time?

Is there a way to let a user write in single and multiple lines in EditText, inputType textMultiline is for Multiline only and text for single line only, is there a way to have both?
Multiline attribute normally allows a user to take advantage of multilines. But incase u want to achieve that, the alternative would ve to use teo edittexts. Give one the multiline attribute and the keep singleline for the other. Since single line takes precidence, you might want to hide the multiline edittext onCreate. Set a length (number of characters) at which you the user should now be switched to the multiline edittext. Save the input added at the time, hide the singleline editText then show the multiline after you have set the original text from the singleline edittext.
I hope that helps.

How to disable TextView automatic text formating for certain characters?

I have textview with droid sans mono font. Max line lenght is 10 characters. gettin it by this function.
this.getPaint().breakText(FULLTEXT, 0, 100, true, this.getWidth(), null);
I want lines always full if there is text and not the break halfway or something similar.
If I am using only normal characters like numbers and letters its working but for characters like "." or "," in certain situations the line break.
For example: if i have line:
"0123456789" or ".012345678"
tv is working as i want.. line is full..but with input:
"Y.012345678"
the line break this way:
".xxxxxxxxx"
"012345678Y"
where x are empty spaces.
i want to have multiline, i just dont want empty spaces (just in last line)
If i insert two special character like:
"01234\\.789"
its start breaking halfway if line is full. how i understand it, textview is breaking sentences which are too long for line.
Curently i am inputing different chareacter without this behaviour, but i would be happy if i didn't need too do this. (too much corections need too be done and its messy)
I noticed that descripted behaviour is not on every android version. I have devices with android 2.3.6, 4.0.4 and kindle fire version. I don't have problem on 2.3.6.
Is there a way how to disable this textview behaviour? thanks.
android:singleLine="true"
is the property of textview and edittext which will keep the text on single line. if length of string is too big then text will be truncated but will never go on next line.

Divide string in lines

I am currently trying to fix my problem with string length at buttons, in android application. So i parse string and then set it as button text. But if one text is bigger then 9 characters or 10 it gets displayed out of the button overlay. I know i could make text smaller but this is not good solution because i already have small text.
So what you guys recommend for example if i have:
String text = "ThisIsSomeRealyLongText";
How can i split this string in 2 lines or when i reach character number 9 just use /n (to break text)?
Using too much text on a button is never a good idea. You should think about using an icon, or shorter text with (if needed) more explanation text somewhere else. Remember,the best UX will be the one with the least amount of reading involved.
Using regex you can do this. It will break the string into 9 characters per line.
text = text.replaceAll("(.{9})", "$1\n");
Use
(new line):
android:text = "ThisIsSome
RealyLongText"
But in code, you can just use \n. You can define in ../res/values/strings.xml:
<string name="longname">ThisIsSome\nRealyLongText</string>
then set the text for you button
android:text="#string/longname"

AWT TextArea seems to count cr-lf sequence as one character

I have this problem:
The text "ABCD\r\nEFGHJ" loaded from a file is matched with java regex "EFGH". Matcher object of course says start of the matched string is in position 6. The matcher counts \r \n as two positions.
I put the original text in a AWT TextArea Component and then call select(6,10) to highlight the area which was matched. Guess what... it starts highlighting from 'F' letter. One position forward than it should...
If more than 1 pair of crlf precedes matched area then highlighting moves even more forward than it should.
Anyone has any simple solution?
Simple solution: remove all \r from the text... :-P
Not as stupid as it sounds, unless you have inconsistent end of lines (it can happen) and want to keep them unchanged... And that's probably what the component does anyway.
I cant mess with the text because it is protocol data and \r and \n characters have semantics that dont have to do with display or line separation. I just want a component that will treat each one input character separately and treat it as one displayed and counted, no matter how it is displayed.
If the \r\n are consistent, you can remove the \r's before running the regex, then replace them before handing off to whatever is next. Or change a copy, if that works better. This way, your regex finds the position in a way consistent with what AWT is expecting.

Categories