Skip to content Skip to sidebar Skip to footer

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 character

Solution 1:

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

Solution 2:

Use SpannableString and BackgroundColorSpan available in android.

Post a Comment for "Highlighting Text Using Html.fromhtml"