is it possible to add
in TextView? Has anyone achieved similar functionality?
I want to have non-breakable space in TextView.
This worked for me:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
textview.setText(Html.fromHtml(your string, Html.FROM_HTML_MODE_LEGACY));
} else {
textview.setText(Html.fromHtml(your string);
}
This is an example that using nbsp in a TextView
<string name="text">Example:\u00A0</string>
TextView
respects the Unicode no-break space character (\u00A0
), which would be a simpler/lighter solution than HTML.
The TextView should respect the non breaking space
<string name="test">Hello world</string>
or
new TextView("Hello\u00A0world");
One unique situation I ran into was adding a non-breaking space to a string resource that took String.format
parameters.
<resources>
<string name="answer_progress" formatted="false">Answered %d of %d</string>
</resources>
I tried to simply copy and past the non-breaking space character into the string and it was replaced with a regular old space after compiling.
Removing the formatted="false", numbering the format args and using the backslash notation worked for me:
<resources>
<string name="answer_progress">Answered %1$d\u00A0of\u00A0%2$d</string>
</resources>
\u00A0
is a non-breaking space, \u0020
is not a non-breaking space