Raghu Nagaraju
Raghu Nagaraju

Reputation: 3288

Highlighting text using Html.fromHtml

I am trying to add background color for the string inside MultiAutoCompleteTextView.

I am overriding replaceText method in multipleAutocompletetextview, trying to replace charactersequence with html like this

@Override
protected void replaceText(CharSequence text) {

    // TODO Auto-generated method stub
    String styledText = "<font color='red'>"+text+"</font>.";
    super.replaceText(Html.fromHtml(styledText));
}

this is working fine. I Can change the font color. But i want to add background color for the font. Can anybody suggest me, how to achieve this?

( The way we are adding tags while creating question, samething i am trying to implement using MultiAutoCompleteTextView. I want to add background for the selected string. )

Thanks in advance

Upvotes: 1

Views: 1408

Answers (2)

user1571573
user1571573

Reputation: 21

Use SpannableString and BackgroundColorSpan available in android.

Upvotes: 1

Thorstenvv
Thorstenvv

Reputation: 5396

Html.fromHtml does not support setting the background color.

You will either have to take the SpannedString returned by Html.fromHtml and set the BackgroundColorSpan to the text you want to set the background color of.

Something like:

new SpannableString(styledText).setSpan(
    new BackgroundColorSpan( Color.YELLOW), 0, styledText.length(),
      Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

See also: http://developer.android.com/reference/android/text/SpannableString.html, http://developer.android.com/reference/android/text/style/package-summary.html

You can as well pass your own TagHandler into Html.fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) to support additional HTML tags. See http://developer.android.com/reference/android/text/Html.TagHandler.html

Upvotes: 3

Related Questions