Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

I have a simple HTML:

<h2>Title</h2><br>

<p>description here</p>

I want to display HTML styled text it in TextView. How to do this?

1 Answer

0 votes
by (40.7k points)

You just need to use Html.fromHtml() to use HTML in your XML Strings. 

Note: If you are simply referencing a String with HTML in your layout XML, it will not work.

Try using the code given below:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

    textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));

} else { 

    textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));

}

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 9, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...