How to disable TextView automatic text formating for certain characters? - java

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.

Related

Get access to the beginning of the second line in TextView

There is a String, which in TextView automatically transfers words to the second line because of the large number of words in one line. I would like to access the beginning of the second line for further editing.
I tried to check for "\n", but it did not work.
What are the possible solutions?

How to create a paragraph of text in java

Is there any way to make a some text appear as a paragraph, since at the moment i use,
private String howtoplay = "[Insert How To Play THe Game Instructions Here]";
When i use strings the text just goes off my game screen which is 800 pixels by 600 pixels.
How do i make it so it doesn't go out of the screen and just either just a block of text, or that when the word reaches the end of the game screen, it will start on a new line.
I have only used strings and so i have been used t using them to create small amounts of text like the title and small commands instructing what button to click.
So in short, is there any way to create a block of text without creating like another class, or just creating a bunch of strings?
EDIT:
So it turns out that i cannot create a new line break, due to the fact i have a drawString
g.drawString(title, ((Sea_InvadersDisplay.WIDTH/2)-(titleWidth/2))-2, (Sea_InvadersDisplay.HEIGHT/2)-123);
and i have found the solution here:
Problems with newline in Graphics2D.drawString
So in other words i have found the solution to my problem.
use can \n to break to the next line and you can use.
everytime you need to move to a new line or new row u just pass \n into your string
Try to use html code.... new line in html code is represented by <br/> tag... so you will have something like this:
<html>
<p>[Insert How To Play THe Game Instructions Here]</p>
</html>

Java tabs ("\t") not working using FileWritter

As you can see, after every text comes a tab, everything is working fine, but after third tab (see output) it generates a space not a tab.
fileWriter = new FileWriter(indexFile, true);
fileWriter.append(id).append("\t");
fileWriter.append(String.valueOf(idx)).append("\t");
fileWriter.append(String.valueOf(pageCount)).append("\t");
fileWriter.append(postal.toUpperCase()).append("\t"); <-- this one
fileWriter.append(address.toUpperCase());
fileWriter.append("\r\n");
My output:
00000347 1 1 FB-6666 DUMMY STREET 1 LAT
The problem comes after "FB-6666".
Any ideas on this?
No, it really is generating a tab - it's just that whatever you're using to view the file is deciding to handle tabs by aligning them to some boundary or other. If you make your postal value FB-6666x I suspect you'll then see a much larger space.
This isn't a problem with the file content at all.
If you want to enforce a certain number of spaces between columns, you'll need to write that many spaces. Alternatively, something else to view the file...
Tabstops don't work this way. As the wikipedia article states,
Tab stops are set manually, and pressing the tab key causes the carriage to go to the next tab stop. In text editors on a computer, the same concept is implemented simplistically with automatic, fixed tab stops.
This means the tabstop will stop at a predefined position in the textfile, not after let's say the space it would take to insert a specific amount of spaces.
It is writing a tab I promise - to convince yourself of this you should either open the resulting file in a hex editor and look at the value in that position, or add another character to the value before the tab and see how the format of the output changes.

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"

Android TextView Define position of forced newline

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.

Categories