Main Tutorials

Android WebView example

Android’s WebView allows you to open an own windows for viewing URL or custom html markup page.

In this tutorial, you will create two pages, a page with a single button, when you clicked on it, it will navigate to another page and display URL “google.com” in WebView component.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. Android Layout Files

Create two Android layout files – “res/layout/main.xml” and “res/layout/webview.xml“.

File : res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/buttonUrl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to http://www.google.com" />
            
</LinearLayout>

File : res/layout/main.xml – WebView example


<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

2. Activity

Two activity classes, an activity to display a button, another activity display the WebView with predefined URL.

File : MainActivity.java


package com.mkyong.android;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private Button button;

	public void onCreate(Bundle savedInstanceState) {
		final Context context = this;

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		button = (Button) findViewById(R.id.buttonUrl);

		button.setOnClickListener(new OnClickListener() {

		  @Override
		  public void onClick(View arg0) {
		    Intent intent = new Intent(context, WebViewActivity.class);
		    startActivity(intent);
		  }

		});

	}

}

File : WebViewActivity.java


package com.mkyong.android;

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

public class WebViewActivity extends Activity {

	private WebView webView;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);

		webView = (WebView) findViewById(R.id.webView1);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.loadUrl("http://www.google.com");

	}

}

3. Android Manifest

WebView required INTERNET permission, add below into AndroidManifest.xml.


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

File : AndroidManifest.xml – See full example.


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mkyong.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".WebViewActivity"
            android:theme="@android:style/Theme.NoTitleBar" />
        
        <activity
            android:label="@string/app_name"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4. Demo

By default, just display a button.

android webview example

Click on button, WebView is display.

android webview example

5. Demo, Again

WebView allow you to manually load custom HTML markup, via webView.loadData(), see modified version :


package com.mkyong.android;

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

public class WebViewActivity extends Activity {

	private WebView webView;

	public void onCreate(Bundle savedInstanceState) {
	   super.onCreate(savedInstanceState);
	   setContentView(R.layout.webview);

	   webView = (WebView) findViewById(R.id.webView1);
	   webView.getSettings().setJavaScriptEnabled(true);
	   //webView.loadUrl("http://www.google.com");

	   String customHtml = "<html><body><h1>Hello, WebView</h1></body></html>";
	   webView.loadData(customHtml, "text/html", "UTF-8");

	}

}

Now, when button is clicked, a custom html page is displayed.

android webview example

Download Source Code

Download it – Android-WebView-Example.zip (16 KB)

References

  1. Official Android webView example
  2. Android WebView Javadoc
  3. Switching Android activity

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
93 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
srrr
2 years ago

zip file isnt getting downloaded

Lavanya Kommana
2 years ago

Thank you soo much it really helped me a lot

Marcos
3 years ago

Kind Sir, how do we simply skip directly to the web page (without a button)?

Thanks in advance!

Ram
5 years ago

One button code ok….more than 6 button in single webview page activity

raj
5 years ago

plzzzzzzzz upload webview with download file audio image video, this webview could not working downloads

Yahoo Support
6 years ago

sir this content is innovative and helpful so thank you so much for sharing it.

Mukesh Yadav
6 years ago

Hello sir

ThAnkyou for this tutorial,

I have a question-
Main actvity XML file have 2 buttons frist btn click then open browser activity and show web page I m set a url in browser activity .But
How to set second button to load diffrent url in same browsers activity
Plz sir reply

Waiting for your reply

Thank you

yusuf
6 years ago

how to enable cookies in webview for android ?

yusuf
6 years ago
Reply to  yusuf

just managed to do it
WebView myWebView = (WebView) findViewById(R.id.wv1);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
myWebView.loadUrl(“website url here/”);

Karsho
8 years ago

Im new for this. Now I want to send push notification to my WebView App. Please, Can you explain like this?

Half Moon
8 years ago
????
8 years ago

hi I need use text in web view for ex. a story for reading thank u .

Nabaasa Gongo
8 years ago

Thanks for the code…….it helped!

Murtaza Panhwar
8 years ago

when i click on button it give me FC error guys help me out 🙁

Corent Sudibyo
9 years ago

hi if i wanna create login page using html and java as code, is it possible?how i send user id n pass from html to java, thx alot

manoj
9 years ago

I am following the same steps…but when i trying to export it automatically deletes the internet permession line…..and because of that…application doeant run….qhat should i do…why this is happening…?

Steve Ridwan
9 years ago

hi, i’m new in android programming, i’m getting error when i create webViewActivity.java

the error is : webview cannot be resolved or is not a field, i try to create that field but not help

here is screen capture: http://postimg.org/image/bhvcqfh39/
i hope you can help me, thanks

Funny Games
9 years ago
polash
9 years ago

nice tutorial.. (Y)

riday
9 years ago
Reply to  polash

yeap

Srihari K
9 years ago

One word. Awesome!

Rejuvenated Horizons
9 years ago

Hi,

I need help with a large text string in string.xml but it shows only the first page but the rest of the text doesn’t show at all. Any tips? — ScrollView is not helping or maybe I am doing something wrong there. Help!

Paritosh
9 years ago

Thanks! Short and Sweet article…

shariq
10 years ago

Chek it man your code is not running. you skip one line here…

add this line in your code……

webView.setWebViewClient(new WebViewClient());

Mugiwara
8 years ago
Reply to  shariq

Thanks for the info

certee
9 years ago
Reply to  shariq

Yes! The code would try to run the website in the device’s internet browser without this code. Thanks!

From
9 years ago
Reply to  shariq

Right shariq ! I’ve done same thing to make the webview able to navigate properly !

Shahmoon Shahzad
10 years ago

Super B 🙂

jgcoroleu
10 years ago

Hi, thanks for your tutorial. How i do for to hide status bar when i run my app?

juan
10 years ago

is it possible to build a java web app such as this in android? how much do you think it would cost? thanks!

Diego
10 years ago

Mk Kong rocks!

Diego
10 years ago
Reply to  Diego

Mk Yong* 🙂

Arun Prabhu
10 years ago

Thank you….

Mahesh sharma
10 years ago

Perfect Example Thanks

Sanmoy Ray
10 years ago

The new webview is a mess (in kitkat). html5 canvas is not hardware accelerated and rendering performance is extremely poor. All games/apps which rely on canvas rendering are slowed down to death. There are multiple bugs raised, but google is completely silent on this mishap.

slow canvas bug :
1. https://code.google.com/p/chromium/issues/detail?id=315111
2. https://code.google.com/p/chromium/issues/detail?id=239864

slow css transformation :
https://code.google.com/p/chromium/issues/detail?id=329108

other issues :
https://code.google.com/p/android/issues/detail?id=62378 (text wrap bug)
https://code.google.com/p/android/issues/detail?id=62220 (broken filechooser)

The problems are so severe that developers are crying foul and asking the old webview back
https://code.google.com/p/android/issues/detail?id=62293

DJW
10 years ago

Hi I am having a problem with this line in MainActivity class

setContentView(R.layout.main);

the R class does not define main?
Any clues?

Kira00
10 years ago
Reply to  DJW

You need to create a layout file called main in your layout folder (res/layout/main.xml)