Android WebView Example

Here you will get android webview example.

Android WebView is a UI component used to open web url or show html data inside the activity.

Below I have given two examples, one is for opening a web url and another is for showing some html data in an activity.

Also Read: Android Load Image from URL (Internet) Example

 

Android WebView Example

Open Web URL

For opening a url we use loadURL() method of WebView class. By default the url is opened in android browser. As we have to open it inside the activity so we have to provide our own WebViewClient.

In this example the activity consists of three UI components namely EditText, Button and WebView. When user will enter url of a website in the edittext and click on button then the website will open in webview.

Create an android project with package name thecrazyprogrammer.androidexample and add following code in respective files.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/url"
        android:hint="Enter the url to open..."/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Open"
        android:id="@+id/openBtn"/>

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>
</LinearLayout>

 

MainActivity.java

package thecrazyprogrammer.androidexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    WebView webView;
    Button openBtn;
    EditText url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView)findViewById(R.id.webView);
        openBtn = (Button)findViewById(R.id.openBtn);
        url = (EditText)findViewById(R.id.url);

        openBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.setWebViewClient(new CustomWebClient());
                webView.loadUrl(url.getText().toString());
            }
        });
    }

    public class CustomWebClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

 

Note: As we are fetching data from internet so we have to provide internet access permission. Just add following line in AndroidManifest.xml file.

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

 

Output

Android WebView Example 1

 

Show HTML Data

The loadData() method of WebView class is used to show html data. It can be done in following way.

Create an android project with package name thecrazyprogrammer.androidexample and add following code in respective files.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>
</LinearLayout>

 

MainActivity.java

package thecrazyprogrammer.androidexample;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity {
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView)findViewById(R.id.webView);
        webView.loadData("<h1>Android WebView Example</h1>","text/html","UTF-8");
    }
}

 

Output

Android WebView Example 2

Comment below if you found anything incorrect or have doubts related to above android webview example.

Leave a Comment

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