Using multiple text colors in Android's textview [ Html.fromhtml() ]

31,424
textview.setText(Html.fromHtml("<i><small><font color=\"#c5c5c5\">" + "Competitor ID: " + "</font></small></i>" + "<font color=\"#47a842\">" + compID + "</font>"));

Try the above.

Share:
31,424
user1029167
Author by

user1029167

Updated on July 09, 2022

Comments

  • user1029167
    user1029167 almost 2 years

    In one textview I'm trying to separate text stylings on different words.

    So far I have:

    textview.setText(Html.fromHtml("<i><small><font color=\"c5c5c5\">" + "Competitor ID: " + "</font></small></i>" + "<font color=\"47a842\">" + compID + "</font>"));
    

    Basically, I'm saying I want "Competitor ID:" to be italic, small, and grey.

    And I want that string compID to be normal sized, not italic, and green.

    The italic and small portions of the code work perfectly, but for some reason, the entire text view has grey text, rather than just "Competitor ID:".

    Any ideas?

  • Muhammad Babar
    Muhammad Babar about 11 years
    @userIsAMonkey +1 great work but why are you using escape sequences in font color if it is in html
  • Evren Ozturk
    Evren Ozturk about 8 years
    For peoples having issues; using color names doesn't work, I think you have to use color codes. wrong:<font color=\"#white\"> right :<font color=\"#f1f1f1\">
  • TWiStErRob
    TWiStErRob over 7 years
    @MuhammadBabar the escape is because this is Java source code. To include a " in a Java String you have to escape it. After Java compiles this it'll look like normal HTML code and will be parsed as such by fromHtml.