简体   繁体   中英

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

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

使用android中可用的SpannableString和BackgroundColorSpan。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM