I have a big string resource (basically the "about" text of the application) which contains styles (such as <b>
, <a>
etc.). The string resource is written on multiple lines, like this:
<string name="about_text"><big><b>About</b></big>\n
Lorem ipsum dolor sit amet and all that stuff...\n
\n
More stuff.</string>
Now, just like in HTML, Android treats actual new lines (not the special \n
) as spaces, so the text ends up looking something like this:
About
Lorem ipsum dolor sit
amet and all that
stuff...
More stuff.
Which looks pretty stupid. Now, I have two options:
- Write the whole thing on one line. I did not go with this because:
- It would be a pretty big line.
- The text needs to be translated, and putting it all on one line would give the translators a great headache.
- Remove the unneeded whitespace programatically.
I went with the second solution, but I'm not sure my implementation is optimal:
CharSequence aboutText = getText(R.string.about_text);
SpannableStringBuilder ssb = new SpannableStringBuilder(aboutText);
for (int i=0; i < ssb.length()-1; i++) {
if (ssb.charAt(i) == '\n' && ssb.charAt(i+1) == ' ') {
ssb.replace(i+1, i+2, "");
}
}
this.aboutText = (TextView) findViewById(R.id.about_text);
this.aboutText.setText(ssb);
It seems very hackish, but I could not find a better way. Is there a better way?
SingleLineTransformationMethod
to the TextView? \$\endgroup\$ – RoToRa Jun 24 '11 at 14:17TextView
interpreting actual new lines as spaces (like in HTML). I basically want the equivalent ofwhite-space: pre
. \$\endgroup\$ – Felix Jun 24 '11 at 15:54