How to load html string in WebView in Android

In this post, we are going to see how to load html String in a WebView in Android
Android WebView is a android UI widget which is used to open any web url or load html data. It is used to show web page in android activity. In simple words, Android WebView is a View that displays web pages.
Example :
Lets say You are creating an android app and you have some user agreement or some web pages hosted online, you can just render it using Android WebView.
There are two methods which we generally use to open web url or load html data.

  • loadUrl
  • loadData
I have already demonstrated about loadUrl in previous post. I am going to demonstrate about loadData in this post.

Source code:

Step 1 :Creating Project

Create an android application project named “WebViewLoadDataExampleApp”.

Step 2 : Creating Layout

Change res ->layout -> activity_main.xml as below:

Step 3 : Creating MainActivity

Change src/main/packageName/MainActivity.java as below:

Hello World from Java2blog!!

Android WebView load html data example

We are getting WebView reference from layout file and then using WebView’s loadData method to load the html String in Android WebView.

Step 4 : Running the app

When you run the app, you will get below screen:

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *