1

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

Use SpannableString and BackgroundColorSpan available in android.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.