I want to display a string with leading whitespace in a Textview. But what I can see is that the android Textview is trimming all leading/trailing whitespace. Is there a way to turn this behavior off?
Old question but if you are not satisfied with "It Does Not trim itself." answer from comments this might help you.
If you are using resource strings with leading or trailing spaces, this is your issue: How to keep the spaces at the end and/or at the beginning of a String?
TextView indeed doesn't trim whitespaces but whatever reads strings from XML does. Use \u0020 instead of spaces.
Related
I'm now searching on Google for weeks, but couldn't find an answer to my problem.
I want my TextView to truncate its text, if it doesn't fit. The problem is, as many words as possible are fitted into the TextView, but I need as many characters as possible.
I found an answer, considering using ellipsize with TruncateAt.MARQUEE, but in this case I can't use TextView.getLayout().getEllipsisStart()to check if the text was truncated or not.
Why do I need this? Well, I want the user to hand over a String for the TextView to display. If it is too long, every possible character should be displayed, to give the user a visual feedback. Additional, the TextView should change color. I think, TruncateAt.MARQUEE would work, if I had a possibility to check for ellipsation happening.
EDIT
The font is fixed. Goal of this mechanic is to 'force' the user to enter a String which will fit.
Is there anyway to display DL DT and DD tags inside a TextView? I tried this
rawString is an array of "<dt>Some Name</dt><dd>Some text</dd>"
StringBuilder builder = new StringBuilder();
builder.append("<dl>");
for (String s : rawString) {
builder.append(s);
}
builder.append("</dl>");
But this doesn't seem to be interpreted even if I use Html.fromHtml() when i set the text
setText(Html.fromHtml(builder.toString()));
I guess if this will never work then is there a way to display information like this in a single textview where we have a 2 column or 3 column data and to line up the columns?
Name: The name
tim: value
yells: yes
If I use Webview then the data looks like this:
Name:
The name
tim:
value
yells:
yes
Seems like there is no way to display this without using a listview
Html.fromHtml() handles a limited set of HTML tags. This blog post of mine from 2010 lists the then-current set, and I am not aware that it has changed much. Quickly eyeballing the source code does not indicate any obvious changes.
So, you have a few choices, off the top of my ever-so-balding head:
Do something other than those tags.
Add a TagHandler to your fromHtml() call and figure out how to set up spans that will format things the way you want.
Use a WebView.
I guess another question would if this will never work then is there a way to display information like this in a single textview:
Getting them aligned like that will be difficult without a monospace font. You might be able to go through some text-measurement stuff to try to determine the right amount of whitespace to add to get things to align, or invent some spans to fill the space. It's one of those things that strikes me as being painful but eventually doable.
Is there any way to carry text in TextView by letters in Android?
If no settings have been set – TextView carries text by words and situation is possible when with too long word you have big whitespace in TextView.
Example – what I’m talking about:
If you can get it at the TextView in XML, I would try giving it the attribute:
android:singleLine
If you have to do it in Java, there are a couple of options to keep it on the same line:
textView.setSingleLine();
textView.setTransformationMethod(new SingleLineTransformationMethod());
If you do want multiple lines, but you want it to break in a way that doesn't split it on the word, you might have to do it manually by analyzing the width of the TextView and how many characters can fit on a line, then inserting newlines appropriately. The two above methods will keep the contents of the TextView on one line, and it'll scroll horizontally. You can look into how this person is doing it.
Another option is to look into the android:ellipsize attribute, but I don't think it'll do what you're looking for.
Not sure if I understood correctly what you want, but I would guess.... if you would like to display the TextView in a single line, without "breaking" the sentence, you should add android:singleLine="true" to the TextView.
Otherwise, you may replace the "_" characters with space, in this case I believe it will carry the text, from the last space.
When I display quoted text in a textview, occasionally the wrapping will happen such that the final quote character (") is wrapped but nothing else is. So I wind up with a dangling quote as the last line, which doesn't look right.
Is there some property to set to keep the text together in these instances? My only thought would be that the period at the end of a quote would get marked as whitespace or the end of a word and so the ui feels free to wrap what comes after.
I was having a problem with dangling colon's in my TextViews, and this bit of code stopped them from wrapping:
tv.setEllipsize(null);
tv.setHorizontallyScrolling(true);
and my unverified stab at the equivalent xml:
android:ellipsize=0
android:singleLine="true"
Change the text font a half-size smaller or something similar to that. This could be an easy fix if not the best solution.
I am in the process of trying to convert a desktop app to Android - and am struggling with some very basic stuff.
When I specify a layout including a textview that holds a sizable amount of text wrap_content seems to arbitrarily break in the middle of a word and I can not find any documentation indicating this can be controlled.
Try useing Ellipsize property of TextView.
"If set, causes words that are longer than the view is wide to be ellipsized instead of broken in the middle"
Just posting this since none of the other solutions worked for me.
I had copied and pasted some text from online and I didn't realize it had weird spacing characters in that caused the text to wrap mid-word rather than at whitespace.
Here is the nasty character " " that looks like a space but isn't actually a space. I did a find and replace for this character and that solved my problem.