Change text color of bold html text

20,260

Solution 1

String htmlBody = "<b style='color:blue !important;'>Pratik</b> has shared photo with <b  style='color:blue !important;'>you</b>.";

Solution 2

Your Solution is:

String styledText = "<b><font color='red'>Pratik</font><b/> has shared photo with <b><font color='red'>you</font></b>";

Solution 3

You have to use like this

public void methodName() {
        String html = "Some text <b>here</b>, and some <b>there</b>";
            String result = html.replace("<b>","<font color=\"#ff0000\"><b>").replace("</b>", "</b></font>");
            textView.setText(Html.fromHtml(result));
    }
Share:
20,260
pratik03
Author by

pratik03

Updated on December 01, 2020

Comments

  • pratik03
    pratik03 over 3 years

    I want to change text color before displaying it to TextView.

    For example,
    I am getting a text from server like

    <b>Pratik<b/> has shared photo with <b>you</b>.
    

    Now the requirements are to display Pratik and you with Bold style and Blue text color. I tried several ways using <span> tag, but I am not getting clear way to display it.

    String htmlBody = "<b>Pratik<b/> has shared photo with <b>you</b>.";
    String formattedBody = "<span>" + htmlBody + "</span><style>b { color: #309ce8 ; } span {font-family: 'Avenir', 'Heavy'; font-size: 17; }</style>";
    
    SpannableString text = new SpannableString(formattedBody);
    tvMessage.setText(text, TextView.BufferType.SPANNABLE); // This is not working as expected.
    
    tvMessage.setText(Html.fromHtml(htmlBody)); // This is not working as expected.
    

    Help me achieve that.

    • Rushi M Thakker
      Rushi M Thakker over 6 years
      You can try simple <font color=#5FCA1C> tag in your html string and then set as Html.fromHtml
    • ND1010_
      ND1010_ over 6 years
      the string u are getting from server Right? with also <b></b> TAG? <b>Pratik<b/> has shared photo with <b>you</b>.
    • Raja
      Raja over 6 years
      See my post it will help you
  • slon
    slon over 6 years
    Try now, I updated it. It looks like something is overriding this style