sattu
sattu

Reputation: 650

changing text color of multiple text strings in same textview

Is to possible to have changing text color of multiple text strings in the same TextView? Using Html.fromHtml is not working:

textViewPrevChat.append("\n"+Html.fromHtml("<b>Bold string </b>")+somestringhere);

It is printing whole thing in bold. Also, the color attribute in <p> is not working.

Upvotes: 0

Views: 183

Answers (1)

Booger
Booger

Reputation: 18725

You should use a SpannableString for this. Here is an example method implementing this:

-This example changes the color of one half of a string, and adjusts the size of another section of the String:

public static SpannableString categoryText(String label, String text, int colorIn) {
        String strIn = label + " " + text;
        SpannableString ss = new SpannableString(strIn);        
        ss.setSpan(new ForegroundColorSpan(colorIn), 0, label.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new RelativeSizeSpan(.80f), 0, label.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        return ss;
    }

Upvotes: 4

Related Questions