Android WebView Tutorial

Author: | Posted in Android, Quick Tips 1 Comment

Basic tutorial for how to use android web view in android application.

In this Android WebView Tutorial we will learn how to implement a basic WebView in Android application.
We will also learn how to load a dynamic web page, How to load a local web page and how to load custom html as String in Android WebView.

First create a new android project. See here for how to create.
After that add a WebView in you layout file(eg.: activity_main.xml).

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />
</RelativeLayout>
and define WebView in your Activity file(eg.: MainActivity.java) like this.
public class MainActivity extends Activity {
    // Declare a webviewobject
    private WebView webview;
    ProgressBar progressBar = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // and define you web view object
        webview = (WebView) findViewById(R.id.webview);
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        // enable javascript so webview can load javascript enabled web pages
        // also
        webview.getSettings().setJavaScriptEnabled(true);
        // set a webviewclient to check loading status of page
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
            }
        });
        webview.loadUrl("<a href="http://www.quicktips.in">http://www.quicktips.in</a>");
    }
}
and finally do not forget to add a permission in your AndroidManifest.xml for internet access to your application project.
    
<uses-permission android:name="android.permission.INTERNET" />

Now run your project on Emulator or Android device with a valid internet connection.

You can see http://www.quicktips.in is loaded in your WebView.

You can also load your own html file(s) in WebView. Place your html file(index.html) in Android asset folder “asset/www/” folder and load using.

webview.loadUrl("file:///android_asset/www/index.html");

Or WebView also allows you to load custom HTML String using

webview.loadData("<html><body><h1>Android WebView Tutorial</h1></body></html>");

 

  • Dharmendra Mishra

    How to download from webview ??