Background
Suppose I use SpannableStringBuilder to add a few things to it, and one of them is a line that I format from a strings.xml file that has an interval inside:
SpannableStringBuilder stringBuilder = new SpannableStringBuilder (); stringBuilder.append(...)... final SpannableString span = new SpannableString(...); span.setSpan(new BackgroundColorSpan(0xff990000), ...,...,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); stringBuilder.append(getString(R.string.string_to_format, span)); stringBuilder.append(...)... textView.setText(stringBuilder);
Problem
Unfortunately, formatting such a line removes the span itself, so in my case there will be no text with background color.
This happens on the line "getString".
What i tried
If I just add span only (without "getString"), it works fine.
I also tried exploring Html.fromHtml, but it does not seem to support the background color for the text.
Question
Is it possible to format a string with a range, but still have a spacing inside?
In particular, the input is line A from the strings.xml file, in which there is only a placeholder (no special HTML tags) and another line B, which should replace the placeholder at run time. Line B should be highlighted for partial text.
In my case, the highlighted text is something to look for in line B.
android textview spannablestring spannablestringbuilder
android developer Jul 04 '16 at 23:25 2016-07-04 23:25
source share