4

I have html text that I need to display in TextView. The html may look like this -

<font color="#AFEEEE"><font style="background-color: rgb(255,140,0);">Text with background and color</font></font>

Html.fromHtml doesn't support any attribute other than color for font tag. But we absolutely must show the background. I could write a custom tag handler but the attributes are not passed in, only the tag is passed in. What is the best way to achieve this ?

NOTE : Cant use Webview.


I tried the code below. If I set raw on the text, it works, but if i process it further and pass it to Html.fromHtml, it doesnt show the background.

public static final String sText =
    "Background on <font style=\"background-color: rgb(255,255,0);\">pa</font>rt text only";

        Pattern pattern = Pattern.compile(BACKGROUND_PATTERN);
        Matcher matcher = pattern.matcher(sText);
        SpannableString raw = new SpannableString(sText);
        BackgroundColorSpan[] spans =
            raw.getSpans(0, raw.length(), BackgroundColorSpan.class);
        for (BackgroundColorSpan span : spans) {
            raw.removeSpan(span);
        }

        while (matcher.find()) {
            raw.setSpan(new BackgroundColorSpan(0xFF8B008B),
                matcher.start(2), matcher.start(2) + matcher.group(2).length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        sText = raw.toString();
        final Spanned convertedHtml =
            Html.fromHtml(sText, ig, new myTagHandler());
1
  • 1
    use jsoup to parse the html in order to access the style object and the background-color in it, then set that color as background to your view. Commented May 14, 2014 at 22:50

2 Answers 2

5
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    String str = "<span style=\"background-color:#f3f402;\">" + TEXT TO HIGHLIGHT + "</span>";
    textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_LEGACY));
} else {
    String str = "<font color='#f3f402'>" + TEXT TO HIGHLIGHT + "</font>";
    textView.setText(Html.fromHtml(str));
}

More - https://stackoverflow.com/a/46035856/3625510

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

1 Comment

This is not a solution to the question author posted. The code you attached is working, whereas the code author has posted contains different attribute, which is not working. This is working: style=\"background-color:#f3f402;\" This is not: style=\"background-color: rgb(255,255,0) The question was how to make it work with style=\"background-color: rgb(255,255,0)
3

Add your own BackgroundColorSpan as you see fit.

Here is some code that sets such a span on all occurrences of a search term within a TextView:

  private void searchFor(String text) {
    TextView prose=(TextView)findViewById(R.id.prose);
    Spannable raw=new SpannableString(prose.getText());
    BackgroundColorSpan[] spans=raw.getSpans(0,
                                             raw.length(),
                                             BackgroundColorSpan.class);

    for (BackgroundColorSpan span : spans) {
      raw.removeSpan(span);
    }

    int index=TextUtils.indexOf(raw, text);

    while (index >= 0) {
      raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
          + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
      index=TextUtils.indexOf(raw, text, index + text.length());
    }

    prose.setText(raw);
  }

So, find your beginning and ending points, create a BackgroundSpan with your desired color, and use setSpan() to apply it.

Note that this assumes that only part of your text needs the background color. If the entire TextView needs the color, go with njzk2's suggestion, and just apply the color to the whole TextView.

7 Comments

While the above solution works, I also need to invoke Html.fromHtml to convert other tags like bold, italics, etc. So can't use the above method before or even after calling Html.fromHtml.
@user1018916: Why not? Spans can overlap AFAIK, and the code above only affects BackgroundColorSpan.
I must be missing something. Html.fromHtml accepts Strings. But if I search and apply the background spans first and then pass it to fromHtml, it doesn't put in the background color. Pasted in my post above.
@user1018916: Html.fromHtml() returns the Spannable that you would then add BackgroundColorSpans to.
I managed to finally get it working. So I search for the regex string before calling Html.fromHtml and convert it into a custom tag and send in the color information in the tag name itself. Then my custom tag handler to Html.fromHtml processes this custom tag and adds BackgroundColorSpan to the text surrounded by my custom tag.
|

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.