YouTube Icon

How to display HTML in TextView in Android?




How to display HTML in TextView in Android?

This is one of those things that has always bothered me, but I never got around to doing and that is being able to view the source code of a website on Android.

For the most part I don’t do any actual coding on my phone, but there are many circumstances where I have to view the source code of a website while on my Android phone.

Also Read:- Google Pixel 3 review: raising the bar for the Android experience

As an SEO, being able to view HTML source code on the fly is very important for me. I am always analyzing new websites, doing proposals and helping people with issues. As an SEO there are many times when I can’t get to a desktop and need to view the HTML or CSS source code on my Android phone.

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 = "http://schemas.android.com/apk/res/android"
   xmlns:tools = "http://schemas.android.com/tools"
   android:id = "@+id/rootview"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:orientation = "vertical"
   tools:context = ".MainActivity">
   <TextView
      android:id = "@+id/htmlToTextView"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content" />
</LinearLayout>

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Bundle;
import android.support.v4.text.HtmlCompat;
import android.support.v7.app.AppCompatActivity;
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>";
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      TextView htmlToTextView = findViewById(R.id.htmlToTextView);
      htmlToTextView.setText(HtmlCompat.fromHtml(htmlText, 0));
   }
}

Also Read:- Apple iPhone XS review: two steps forward, one step back

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

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 RunPlay Icon  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above example it showing HTML tags a string.

Also Read:- How to become a superhero on android



Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.

CrowdforGeeks

CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.

CrowdforThink

Our mission is "Har Koi Dekhe Video, Har Ghar Dekhe Video, Ghar Ghar Dekhe Video" so we Provide videos related to Tutorials, Travel, Technology, Wedding, Cooking, Dance, Festivals, Celebration.

Apna Video Wala
CFT

News & Blogs

58de4563a561421b9d27a5f44972b306.jpg

Top Mobile App Development Trends You Absolutel...

While none of us could have predicted there would be a global pandemic in early 2020, experiencin...

cf72de50fea16ff9b8476b6ecb323b2c.jpg

Android apps which very help full in our daily ...

There are lots of android apps which very help full in our daily life. We stay in an era&nb...

b8fbee2676d885da97919a03b850cdba.png

How to Send Push Notification to Android Using ...

Fundamentally, we have utilized GCM(Google Cloud Messaging) for sending pop-up message to Android...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

I’m Mertin Wilson a technician in a camera company and certified expert of different P...

Mertin Wilson

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman

Pankaj Singh is a Senior Digital Marketing Consultant with more than 2 years of experience in SEO...

Pankaj Singh
CFT

Our Client Says

WhatsApp Chat with Our Support Team