Issue
I used to be able to do this:
<string name="foo">white <font fgcolor="#ff6890a5">blue</font></string>
But now it doesn't work any more. It seems to be a bug in the integer parsing code; see https://code.google.com/p/android/issues/detail?id=58192
Problem is, I'm getting customer complaints now; I can't wait for the bug to be fixed.
Does anybody know a work-around, such as using named resources from color.xml or something like that?
ETA: I've discovered fgcolor="blue"
still works, but it's the wrong shade of blue. Is there a list of legal color names somewhere? Maybe I could find one that's close enough. It also works if the color is a number without the high bit set, like #7f6890a5
, but of course that's too faint to be useful; I need a solid color, not semi-transparent.
ETA: browsing source code shows these colors:
aqua 0x00FFFF
black 0x000000
blue 0x0000FF
fuchsia 0xFF00FF
green 0x008000
grey 0x808080
lime 0x00FF00
maroon 0x800000
navy 0x000080
olive 0x808000
purple 0x800080
red 0xFF0000
silver 0xC0C0C0
teal 0x008080
white 0xFFFFFF
yellow 0xFFFF00
This doesn't fix my problem, but perhaps other people searching on this question could find this information useful.
Solution
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) {
final SpannableString s = new SpannableString(text);
final ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class);
for (final ForegroundColorSpan oldSpan : spans) {
final ForegroundColorSpan newSpan = new ForegroundColorSpan(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.
Answered By - Joe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.