Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.fromHtml() not working with background color of Text

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?

like image 903
Pratik Butani Avatar asked Jan 04 '14 11:01

Pratik Butani


Video Answer


1 Answers

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).

like image 122
Sanket Kachhela Avatar answered Sep 27 '22 18:09

Sanket Kachhela