Loading HTML Code Into An Android Application

When developing an Android application, it often happens that you need to display HTML code within it. This HTML code can be static or come from a URL. In both cases, the approach is almost the same and we will see in this tutorial how to integrate static HTML code into an Android application.

Image for post
Image for post

You can also discover this tutorial in video on YouTube:

Loading The HTML Code

For loading and rendering HTML Code into an Android application, the best solution is to use the WebView component available in standard in the Android SDK.

So, inside a MainActivity class, we create a WebView object. The, we enable JavaScript support for this instance by calling the setJavaScriptEnabled method with true in parameter for the WebSettings object associated to our WebView.

Then, we set a WebViewClient instance for our WebView. Next step is to create some HTML content like that:

Finally, we have to load this HTML content inside our WebView by calling its loadData method. It gives us the following complete code for our MainActivity class:

At the end of activity, you must not forget to call the destroy method of the WebView instance for avoiding potential memory leaks in your Android application.

Our Android Application In Action

Last step of the tutorial is to pur our Android application in action. So, once the application is launched, you should see the HTML Code display at the screen:

Image for post
Image for post

That’s all for that tutorial. Simple but efficient!

To Go Further

To go further, you should discover how to load an external Website from an URL into an Android application. For that, you have just to go reading the following tutorial:

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store