Android Html.fromHtml(String) doesn't work for text

后端 未结 12 1456
逝去的感伤
逝去的感伤 2020-12-23 19:32

I am trying to add lines with different colors to my TextView using html tags.

For whatever reason,

    Html.fromHtml(\"

        
相关标签:
12条回答
  • 2020-12-23 20:14
    txt_description1.setText(Html.fromHtml("<font color='rgb'>"+str_description1+"</font>"));
    

    If you do not want a single static color and want to directly reflect from editor you can use "rgb". It will reflect the exact color what you have set in editor, just set in textview and concat it with textview value. And you are all set to go.

    0 讨论(0)
  • 2020-12-23 20:15

    I use this code

    Html.fromHtml(convertToHtml("<font color='#145A14'>text</font>"));
    
    public String convertToHtml(String htmlString) {
    
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<![CDATA[");
        stringBuilder.append(htmlString);
        stringBuilder.append("]]>");
        return stringBuilder.toString();
    }
    
    0 讨论(0)
  • 2020-12-23 20:15
    textView.setText(Html.fromHtml("<font color='blue'>text</font>"));
    
    0 讨论(0)
  • 2020-12-23 20:18
    Html.fromHtml("<font color='#145A14'>text</font>");
    

    Instead of above please use following

    Html.fromHtml("<![CDATA[<font color='#145A14'>text</font>]]>");
    

    This worked for me and I am sure it will also work for you.

    Let me know in case of any issue.

    0 讨论(0)
  • 2020-12-23 20:27

    Make sure your RGB value is CAPITALIZED. Android can understand #00FF00 but not #00ff00.

    0 讨论(0)
  • 2020-12-23 20:27

    For color text with hyperlink URL:

    textView.setText(Html.fromHtml("You agree to our <font color='#F20000'><a href='https://www.yoururl.com'> Terms of Service</a></font> and <font color='#F20000'><a href='https://www.yoururl.com'>Privacy Policy</a></font>", Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);
    

    This worked for me and I am sure it will also work for all.

    0 讨论(0)
提交回复
热议问题