How to display HTML in TextView in Android?

AndroidMobile DevelopmentProgramming

In Some situations, we should need to show HTML as text in android. Here is the simple solution to show HTML in TextView in android.

Step 1 − Create a new project in Android Studio,go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = ""
   xmlns:tools = ""
   android:id = "@+id/rootview"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical"
   tools:context = ".MainActivity">
      android:id = "@+id/htmlToTextView"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />

Step 3 − Add the following code to src/

package com.example.andy.myapplication;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   String htmlText = "<h2>What is Android?</h2>\n" + "<p>Android is an open source and Linux-based <b>Operating System</b> for mobile devices such as smartphones and tablet computers.
Android was developed by the <i>Open Handset Alliance</i>, led by Google, and other companies.</p>\n" + "<p>Android offers a unified approach to application development for mobile devices which means developers need only develop for Android, and their applications should be able to run on different devices powered by Android.</p>\n" + "<p>The first beta version of the Android Software Development Kit (SDK) was released by Google in 2007 whereas the first commercial version, Android 1.0, was released in September 2008.</p>";
   protected void onCreate(Bundle savedInstanceState) {
      TextView htmlToTextView = findViewById(;
      htmlToTextView.setText(HtmlCompat.fromHtml(htmlText, 0));

In the above example, we kept HTML tags in a string as htmlText and added string to textview as shown below.

htmlToTextView.setText(HtmlCompat.fromHtml(htmlText, 0));

In the above code we are taking HTML data from fromHtml() and appending to textview using setText() . 0 is a flag. you can assign flag as per project resource.

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run Play Icon  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Android Description

In the above example, it showing HTML tags a string.

Published on 05-Dec-2018 12:30:29