HTML
TextView
Android Development
Coding Tips
User Interface Design

How to display HTML in TextView?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Displaying HTML content in a TextView on Android is a common requirement for many developers. Android provides built-in support to handle simple HTML content but it requires using the Html.fromHtml() method properly, along with understanding its limitations and enhancements through Spannables for more complex HTML or CSS styles.

Understanding HTML and TextView

TextView is a basic UI component in Android, primarily used to display text. If your application needs to display styled text or HTML, Android's TextView can handle basic tags but it's not intended for complex HTML rendering, which would be better handled by a WebView.

Using Html.fromHtml()

The Html.fromHtml() method is the quickest way to load HTML content into a TextView. This method converts formatted HTML strings into displayable styled text. However, it only supports a subset of HTML tags.

Basic Usage

java
TextView textView = (TextView) findViewById(R.id.myTextView);
String htmlString = "<h2>Title</h2>&#10;<p>Description here</p>";
textView.setText(Html.fromHtml(htmlString));

Note: From Android N (7.0, API level 24) and onwards, you should use the Html.fromHtml(String source, int flags) method:

java
textView.setText(Html.fromHtml(htmlString, Html.FROM_HTML_MODE_LEGACY));

The second parameter is for handling image loading and future-proofs code but mainly controls how HTML tags are handled.

Supported HTML Tags

The fromHtml() method supports basic tags such as:

  • <b> for bold text
  • <i> for italic text
  • <u> for underlined text
  • <big> for larger text
  • <small> for smaller text
  • <sup> for superscript
  • <sub> for subscript
  • &#10; for line breaks
  • <p> for paragraphs

This method will strip any tags that are not supported and show the text without formatting.

Handling Images and Lists

Displaying images or lists requires additional considerations. For images, the Html.ImageGetter interface can be used, while lists require a custom handling approach since they're not supported directly.

Using Html.ImageGetter to Display Images

You can embed images in a TextView by implementing the ImageGetter interface, which is responsible for fetching and returning the drawable for an image source.

java
1Html.ImageGetter imageGetter = new Html.ImageGetter() {
2    public Drawable getDrawable(String source) {
3        Drawable drawable = ... // load the image
4        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
5        return drawable;
6    }
7};
8textView.setText(Html.fromHtml(htmlString, Html.FROM_HTML_MODE_LEGACY, imageGetter, null));

Enhancements with Spannables

Spannables are powerful and allow much more detailed text styling and interactions within a TextView. You can use SpannableStringBuilder to mix and match different styles, handle clicks, or add custom fonts and colors dynamically.

Conclusion and Best Practices

While Html.fromHtml() and TextView are good for simple HTML rendering, they have limitations:

  • No CSS support
  • Limited HTML tag support
  • Not suitable for complex HTML pages

For more complex HTML or when CSS styling is involved, consider using a WebView, which offers full browser-like rendering capabilities.

Summary Table

FeatureSupported in TextView with Html.fromHtml()Notes
Bold, Italic, UnderlineYesBasic formatting supported
ImagesYes, with Html.ImageGetterRequires custom implementation
ListsNoMust be handled manually or ignored
CSSNoNot supported, use WebView instead
Advanced InteractionNoUse Spannables for advanced features

Using these insights, you can choose appropriate methods and components for displaying HTML content in your Android applications, balancing between simplicity and the richness of HTML.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.