Reputation: 7866
I have found some solutions on how to change the color of specific characters and words of a string stored in strings.xml, however, it is not working for me. I have the following string
<string name="my_string">
Humpty Dumpty sat on a <font fgcolor="#FF0000">wall</font> Humpty Dumpty had a great <font fgcolor="#FF0000">fall</font></string>
I was expecting the words wall and fall to be the color of #FF0000 (red), however, they do not display at all. After further research I learned that you have to declare the alpha channel, so then I updated my string to
<string name="my_string">
Humpty Dumpty sat on a <font fgcolor="#FFFF0000">wall</font> Humpty Dumpty had a great <font fgcolor="#FFFF0000">fall</font></string>
My declared alpha channel is fully opaque, yet the results remain the same.
Lastly, I also tried using CDDATA, this is my attempt with that.
<string name="my_string"><![CDATA[
Humpty Dumpty sat on a <font color=\'#FF0000\'>wall</font> Humpty Dumpty had a great <font color=\'#FF0000\'>fall</font>]]</string>
What else am I doing wrong? Thanks in advance!
Upvotes: 3
Views: 9824
Reputation: 366
As Html.from() is deprecated in android now so, the latest code for achieving this in kotlin is:-
In string.xml:
<string name="by_continuing_you_agree_to_tc"><![CDATA[By continuing, you agree and acknowledge you\'ve read our <font color=\'#F1BC47\'>Terms and Conditions</font> to XYZ organisation]]></string>
In Fragment/class:
textview.text = getString(R.string.by_continuing_you_agree_to_tc).toHtml()
Extension function
fun String?.toHtml(): Spanned? {
if (this.isNullOrEmpty()) return null
return HtmlCompat.fromHtml(this, HtmlCompat.FROM_HTML_MODE_LEGACY)
}
And you are done. :) Happy Coding..
Upvotes: 1
Reputation: 2289
In string.xml, simply just use font color property
<string name="hello_world"><font color="#your_color_in_hexa"> Hello</font> world!</string>
This above code will color the "Hello" part of the text
Upvotes: 0
Reputation: 10143
Prior to 4.x, you could do this:
<string name="my_string">
Humpty Dumpty sat on a <font fgcolor="#FFFF0000">wall</font>
However, bug https://code.google.com/p/android/issues/detail?id=58192 broke this functionality because it introduced an integer parser that can't handle numbers with the highest bit set, and unfortunately you can't omit the opacity part of the color (which most people would prefer to set to ff as in this example.)
I just yesterday learned a clever work-around. What you do is negate the hex color value in two's complement. How you do this depends on your hex calculator, but the easiest way is to subtract your color from 0x100000000. In your case, that would result in 0x100000000 - 0xffff0000 = 0x10000. (Or you could just invert it, e.g. 0000ffff which would be close enough). You then negate this again with a minus sign:
<string name="my_string">
Humpty Dumpty sat on a <font fgcolor="-#10000">wall</font>
and voilla! you have your desired color.
Kudos to TWiStErRob for figuring this out in https://stackoverflow.com/a/11577658/338479
ETA: I just discovered that this will crash your app if you do it on a 2.x system; it throws a NumberFormat exception.
Upvotes: 3
Reputation: 2264
Optionally you can use Spannable
http://developer.android.com/reference/android/text/Spannable.html
Upvotes: 0
Reputation: 15424
You're suppose to use something like this.
textView.setText(Html.fromHtml(getString(R.string.some_text)));
and your strings.xml
should contain the string as follows.
<string name="some_text"><![CDATA[<font color=\'#cc0029\'>Some</font> random <font color=\'#ffcc00\'>text</font>]]></string>
Check here for more details.
Upvotes: 10