How Can I Get The Fgcolor Attribute To Work On Recent Android Versions?
Solution 1:
How about we leverage the fact that colors without the high bit set still works and just replace it with the correct colors? So you can have a method like this:
private CharSequence fixSpanColor(CharSequence text) {
if (text instanceof Spanned) {
finalSpannableStrings=newSpannableString(text);
final ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class);
for (final ForegroundColorSpan oldSpan : spans) {
finalForegroundColorSpannewSpan=newForegroundColorSpan(oldSpan.getForegroundColor() | 0xFF000000);
s.setSpan(newSpan, s.getSpanStart(oldSpan), s.getSpanEnd(oldSpan), s.getSpanFlags(oldSpan));
s.removeSpan(oldSpan);
}
return s;
} else {
return text;
}
}
You will then need to pass any text with the required color accent through this method, the simplest example would be modifying all calls like this:
tv.setText(getText(R.string.foo));
To:
tv.setText(fixSpanColor(getText(R.string.foo)));
Hopefully, depending on how your code is structured, there might already a central place where you can add this extra method call.
Solution 2:
I have some awful workaround in my client code to manually reset the ForegroundColorSpans to the proper color, but it would be great not to have to do so.
I think the workaround that the issue reporter's talking about is the following:
Define the string as:
<string name="foo">white blue</string>
In your activity:
TextViewtv= (TextView) findViewById(R.id.textView1);
SpannableStringspannableString=newSpannableString(getResources().getString(R.string.foo));
ForegroundColorSpanfcs=newForegroundColorSpan(getResources().getColor(R.color.bluish));
spannableString.setSpan(fcs, spannableString.toString().indexOf(" ") + 1,
spannableString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
tv.setText(spannableString);
R.color.bluish
is defined as <color name="bluish">#ff6890a5</color>
.
But, using " " (space) to distinguish and apply the ForegroundColorSpan
would only be practical if you have a small number of strings defined.
The following modification might actually be easier for you to carry out:
Define the string as:
<stringname="foo_sep_1"><![CDATA[white <font color=\"#6890a5\">blue</font>]]></string>
Or:
<stringname="foo_sep_1">white <font color="#6890a5">blue</font></string>
In your activity:
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setText(Html.fromHtml(getResources().getString(R.string.foo_sep_1)));
Be careful about the color code: HTML color codes do not have alpha
values (RRGGBB
will work, AARRGGBB
will not)
Another workaround is using Html.fromHtml(String)
directly:
TextView tv3 = (TextView) findViewById(R.id.textView3);
tv3.setText(Html.fromHtml("white <fontcolor='#6890a5'>blue</font>"));
Solution 3:
Kudos to TWiStErRob who found a solution that doesn't involve code in https://stackoverflow.com/a/11577658/338479. To quote:
for any color above 7fffffff apply the following: <font color="#ff6890a5"> put ff6890a5 into a calculator (optionally convert to decimal first) and flip the sign, then (optionally convert back to hexa) take the last 8 hexadecimal digits and use <font color="-#00976F5B">.
Post a Comment for "How Can I Get The Fgcolor Attribute To Work On Recent Android Versions?"