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.

Click on button, WebView is display.

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.


Javascript not working with this, though i think it is enabled … what could be the problem…
Hello sir how to display latex string in webview ?using mathjax plz hellp me
thank you very much all codes are working well
thank you
hello sir I want to give print command to my output how can i give it… the java script code function window.print() does’nt give me the dialog box of available printer
String customHtml = ” window.print() Menu Order “;
if want to print the output to the local printer how can i do it.. window.print() java script function doesn’t work..
plz help me out with source code
How to check whether Wi-Fi internet access is active or not in a device. Take a look at the post:
http://android.programmerguru.com/android-check-wifi-internet-connection/
i ve downloaded ur code & run it but it displays only second screenshot posted above not d first wer it displays google.com homepagwhat to do..???
plz help
Hey! i’ve downloaded your project & tried it onto Eclipse. There is a problem, when i click
it starts the default Android Browser… Please help me out!!
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
// this is set webview in ur activity
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(“http://www.google.com”);
Hi i have html form with input type=”radio” but cant view it in webview
other things are visible of table
Hi,
A bit cheeky this one :-) – please could you provide me with code (and its location – filename) that I can use to launch a url. I have created the graphics – just need to launch the url when icon selected.
Many thanks
Lester
Hi,
I want to draw something on WebView. How can i acheive that? Please help
Kinda new with Android, but was wondering how do I skip the “press button to go to app” and just directly go to WebView function. Thank you
yaa
how is that ?
Hi. Excelent example, many thanks, now one question… Im have a webpage and like the application load my Webpage, then modidy the url in webView.loadUrl(“”); and load fine. Now the problem is when make click in any link of the my Web because my application launch the Navigator and not open the webpage into my app. How can make this? Load only in my app my Webpage? Thanks for your help
you need to override the default application that handles URLs. you can do this by adding this 2 lines to your WebViewActivity.java class.
Very useful is this Your tutorial. Thank you. And maybe You know a way to webChromeClient?
Hi sorry for my english. Could anyone help me? I have a page html that I have to show with a webview. In this html there are references to youtube videos. How can I show them into browser when I click on their previews?
Thanks
Hi am getting this error when i am launch the webview from listview activity.
See interesting thing is when i am try to launch webview two or three time it’s work fine. After am getting this error. Please give me perfect solution .
All ideas are welcome
thanks, I was looking for that second method.
Thanks for the WebView tutorial too. I managed to solve 2 things in a day because of your tutorials. :)
sir
please, tell me how to i use hyperlink ….by which i can connect with from list view to webview. (for read pdf material)
So you could set up an html form in a WebView but how do you collect user responses in your Android code? it exists anything similar to request.getParameter() in Android?
thanks!!
can i return first application with “back” button in the second application? If I can, how?
How to set the captured image in Android phone as wallpaper programatically??
Take a look at this post:
http://android.programmerguru.com/simple-camera-activity/
Great job….
Hi all ,
My First Activity works well , but in second activity while i click Connect button, it is showing me as Force to Shut down .. Same code i worked . Wats the problem . Can you temme pls?
Hi ,
I am very new to Android , i created the program and in first activity it showing me as Connect the link , later in second activity it telling me to force down the emulator . My doubt is , how should i check Internet and Connection of Internet in ANDROID ? Please anyone help me ??
I tried the example but I am getting blank screen on clicking the button.
Log has the following content -
07-05 10:24:03.924: W/webcore(1224): java.lang.Throwable: EventHub.removeMessages(int what = 107) is not supported before the WebViewCore is set up.
07-05 10:24:03.924: W/webcore(1224): at android.webkit.WebViewCore$EventHub.removeMessages(WebViewCore.java:1683)
07-05 10:24:03.924: W/webcore(1224): at android.webkit.WebViewCore$EventHub.access$7900(WebViewCore.java:926)
07-05 10:24:03.924: W/webcore(1224): at android.webkit.WebViewCore.removeMessages(WebViewCore.java:1795)
07-05 10:24:03.924: W/webcore(1224): at android.webkit.WebView.sendOurVisibleRect(WebView.java:2917)
07-05 10:24:03.924: W/webcore(1224): at android.webkit.ZoomManager.setZoomScale(ZoomManager.java:593)
07-05 10:24:03.924: W/webcore(1224): at android.webkit.ZoomManager.access$1700(ZoomManager.java:49)
07-05 10:24:03.924: W/webcore(1224): at android.webkit.ZoomManager$PostScale.run(ZoomManager.java:984)
07-05 10:24:03.924: W/webcore(1224): at android.os.Handler.handleCallback(Handler.java:605)
07-05 10:24:03.924: W/webcore(1224): at android.os.Handler.dispatchMessage(Handler.java:92)
07-05 10:24:03.924: W/webcore(1224): at android.os.Looper.loop(Looper.java:137)
07-05 10:24:03.924: W/webcore(1224): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-05 10:24:03.924: W/webcore(1224): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 10:24:03.924: W/webcore(1224): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 10:24:03.924: W/webcore(1224): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-05 10:24:03.924: W/webcore(1224): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-05 10:24:03.924: W/webcore(1224): at dalvik.system.NativeStart.main(Native Method)
I am getting this error as well
07-05 17:20:30.623: E/chromium(1243): external/chromium/net/disk_cache/backend_impl.cc:2022: [0705/172030:ERROR:backend_impl.cc(2022)] Corrupt Index file
Please comment if the following program works or not.
This is about to create an icon, then after clicking on the icon will bring to another html page. That html page will bring to other html pages.
package com.xxxxx.xxxxx.xxxx;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.view.View.onClickListener;
public class UTActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startActivity()
{
Intent launchActivity = new Intent (this, UTActivity.class);
launchActivity).loadUrl(“file:///xxxxxxxxx.html”);
}
Button button = (Button) findViewById(R.id.trigger);
button.setOnClickListener(new View.onClickListener()
{
public void onClick(View view) {
startActivity();
}
} );
}}
hello. i have a website, with some live wallpaper apk’s. so i created app with webview on my site, but when i tryng to download an apk, looks like its nothing happening. so how do i enable direct download on webview. code looks like this:
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class FootiepalActivity extends Activity {
private WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.loadUrl(“http://www.exemple.com”);
mWebView.setWebViewClient(new HelloWebViewClient());
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Please anyone????
This is an amazing tutorial. But what if I want to use Javascript to make the application more interactive ?? I tried form developers site..but they only gave it to flip the images…when I tried with buttons and text boxes..it never worked for me :(
Please help !! thanks in advance !!
Vera
you can trying with phonegap