---
title: "WebView in Android"  
description: "WebView is an android UI component that is used in different purpose like display a web page, display online components, display a HTML Content and ap"  
author: "Arti Mishra"  
published: 2018-04-17  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12868/webview-in-android  
category: "android apps"  
tags: ["android", "webview"]  
reading_time: 2 minutes  

---

# WebView in Android

WebView is an **android UI component** that is used in different purpose like display a **web page, online components, displaying HTML Content and apply HTML Formatting on your android app.** \

Generally, it is used to **display web page** in android. And android web page can be loaded from same application or project. For displaying a webpage in android webview uses **webkit engine**. And for loading data, android provided two methods loadUrl() and loadData() to display a content on webpage. These are some uses of WebView-

## 1. Display online content-

WebView myWebView = (WebView) findViewById(R.id.webView1);

myWebView.loadUrl(“https://www.mySite.com”);

## 2. Display HTML Webpage -

myWebView.loadUrl(locationOfHTMlFile);

## 3. Displaying HTML Formatting-

String data =”<html><body><i><h1>WebView Example </h1></i></body></html>”;

mywebview.loadData(data,”text/html”,”utf-8”);

## Android WebView Example :

Provide internet permission on AndroidManifest.xml file.

```
<uses-permission android:name="android.permission.INTERNET" /> 
```

\

**activity_main.xml** \

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.msclient009.webviewexample.MainActivity">
    <WebView
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:id="@+id/webView1" />
    <WebView
        android:id="@+id/webView2"
        android:layout_width="match_parent"
        android:layout_height="435dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="233dp" />
</LinearLayout>
```

## \

## ActivityMain.java

```
package com.example.msclient009.webviewexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView1=(WebView)findViewById(R.id.webView1);
        WebView webView2=(WebView)findViewById(R.id.webView2);
        String data = "<html><body><h1 style='align:center;color:red'><i>WebView Examle<i></h1><div style='height:5;width:100%;background:#00f;align:center;fontStyle:bold;color:white;'>Hello</div></body></html>";
        webView1.loadData(data, "text/html", "UTF-8");
        webView2.loadUrl("https://www.mindstick.com");
    }
}
```

## \

## Output :

![WebView in Android](https://www.mindstick.com/mindstickarticle/2e6b22d9-f8f6-4de7-9950-8cdc3c8c5f08/images/eba3e23b-a8e2-431d-abf7-f686d56ca41e.png)**\**

## \

## \

---

Original Source: https://www.mindstick.com/articles/12868/webview-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
