I am trying to set a background in a TextView
using Html.fromHtml()
. In particulare, I want to set the background on the first word.
I have used the following code:
Html.fromHtml("<font color='red'>("+someText+")</font>");
and it is executing successfully with text color. However I want to change the background color.
How can I do this?
Try this:
TextView TV = (TextView) findViewById(R.id.mytextview01);
Spannable wordtoSpan = new SpannableString("hello hi. how are you?");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
wordtoSpan.setSpan(new BackgroundColorSpan(Color.RED), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(wordtoSpan);
This is to set both text and background color (the latter with BackgroundColorSpan
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With