Highlighting Text Color using Html.fromHtml() in Android?

116,761

Solution 1

Or far simpler than dealing with Spannables manually, since you didn't say that you want the background highlighted, just the text:

String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

Solution 2

Using color value from xml resource:

int labelColor = getResources().getColor(R.color.label_color);
String сolorString = String.format("%X", labelColor).substring(2); // !!strip alpha value!!

Html.fromHtml(String.format("<font color=\"#%s\">text</font>", сolorString), TextView.BufferType.SPANNABLE); 

Solution 3

This can be achieved using a Spannable String. You will need to import the following

import android.text.SpannableString; 
import android.text.style.BackgroundColorSpan; 
import android.text.style.StyleSpan;

And then you can change the background of the text using something like the following:

TextView text = (TextView) findViewById(R.id.text_login);
text.setText("");
text.append("Your text here");
Spannable sText = (Spannable) text.getText();
sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);

Where this will highlight the charecters at pos 1 - 4 with a red color. Hope this helps!

Solution 4

Alternative solution: Using a WebView instead. Html is easy to work with.

WebView webview = new WebView(this);

String summary = "<html><body>Sorry, <span style=\"background: red;\">Madonna</span> gave no results</body></html>";

webview.loadData(summary, "text/html", "utf-8");

Solution 5

 String name = modelOrderList.get(position).getName();   //get name from List
    String text = "<font color='#000000'>" + name + "</font>"; //set Black color of name
    /* check API version, according to version call method of Html class  */
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
        Log.d(TAG, "onBindViewHolder: if");
        holder.textViewName.setText(context.getString(R.string._5687982) + " ");
        holder.textViewName.append(Html.fromHtml(text));
    } else {
        Log.d(TAG, "onBindViewHolder: else");
        holder.textViewName.setText("123456" + " ");   //set text 
        holder.textViewName.append(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));   //append text into textView
    }
Share:
116,761
sunil
Author by

sunil

I am Mobile Developer and have developed on J2ME, Blackberry and Android.

Updated on July 08, 2022

Comments

  • sunil
    sunil almost 2 years

    I am developing an application in which there will be a search screen where user can search for specific keywords and that keyword should be highlighted. I have found Html.fromHtml method.

    But I will like to know whether its the proper way of doing it or not.

    Please let me know your views on this.

  • Michał Klimczak
    Michał Klimczak over 11 years
    It's worth noting, that Html.fromHtml is slower than SpannableString, because it involves parsing. But for a short text it doesn't matter
  • Kenneth Evans
    Kenneth Evans over 11 years
    There appears to be another solution at link. See the answer by Legend.
  • speedynomads
    speedynomads about 11 years
    nicely done. useful to be able to use colors from your apps resources.
  • James
    James about 10 years
    Just as a comment, I found I didn't need to pass TextView.BufferType.SPANNABLE and it still worked. Thanks!
  • mehmet
    mehmet almost 10 years
    do you know any html editor for arrenging long text for android. for example I am trying to use this site (html.am/html-editors/html-text-editor.cfm) but if I want to see source it returns color as <span style="color:#ff0000;"> so that android dont change the color.
  • Christopher Orr
    Christopher Orr almost 10 years
    @mehmet You could presumably just search and replace to change the <span> tags to <font> tags with a "color" attribute.
  • Anand Savjani
    Anand Savjani about 8 years
    you save my day and this is working for hint text also.
  • marcolav
    marcolav about 6 years
    I want to stress second line, where you substring the color hex: alpha value would make the color setting invalid! Don't forget it!
  • Johnny Five
    Johnny Five about 5 years
    Clarification for those who wonders how to use specific hex color in html: <font color='#aabbcc'>text</font>
  • Mateen Chaudhry
    Mateen Chaudhry about 5 years
    @JohnnyFive I am trying to change <a tag text color. But it is not working for me. It always set color to app accent color. any suggestion?
  • Noor Hossain
    Noor Hossain over 3 years
    how to get the font color from color.xml ?
  • Phlip
    Phlip about 3 years
    because of fromHTML() limitations, I'm heading for a tablet app with text in every panel displayed in a WebView. Welcome to the 2020s, where everything's a web page including your tablet's text view!~
  • Khyati Vara
    Khyati Vara almost 3 years
    int color = getResources().getColor(R.color.any_color_name);