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.
zip file isnt getting downloaded
Thank you soo much it really helped me a lot
Kind Sir, how do we simply skip directly to the web page (without a button)?
Thanks in advance!
One button code ok….more than 6 button in single webview page activity
plzzzzzzzz upload webview with download file audio image video, this webview could not working downloads
sir this content is innovative and helpful so thank you so much for sharing it.
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
how to enable cookies in webview for android ?
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/”);
Im new for this. Now I want to send push notification to my WebView App. Please, Can you explain like this?
Find more webview tutorial here
hi I need use text in web view for ex. a story for reading thank u .
Thanks for the code…….it helped!
when i click on button it give me FC error guys help me out 🙁
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
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…?
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
Android tutorial
https://www.youtube.com/watch?v=iXvbv5bSbaY
nice tutorial.. (Y)
yeap
One word. Awesome!
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!
Thanks! Short and Sweet article…
Chek it man your code is not running. you skip one line here…
add this line in your code……
webView.setWebViewClient(new WebViewClient());
Thanks for the info
Yes! The code would try to run the website in the device’s internet browser without this code. Thanks!
Right shariq ! I’ve done same thing to make the webview able to navigate properly !
Super B 🙂
Hi, thanks for your tutorial. How i do for to hide status bar when i run my app?
is it possible to build a java web app such as this in android? how much do you think it would cost? thanks!
Mk Kong rocks!
Mk Yong* 🙂
Thank you….
Perfect Example Thanks
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
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?
You need to create a layout file called main in your layout folder (res/layout/main.xml)