pratik03
pratik03

Reputation: 661

Change text color of bold html text

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.

Upvotes: 4

Views: 44606

Answers (3)

Raja
Raja

Reputation: 2815

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));
    }

Upvotes: 1

slon
slon

Reputation: 1030

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

Upvotes: 9

Dhaval Kasundra
Dhaval Kasundra

Reputation: 61

Your Solution is:

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

Upvotes: 3

Related Questions