0% found this document useful (0 votes)
691 views

Android WebView Example PDF

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.

Uploaded by

Shoaib Quraishi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
691 views

Android WebView Example PDF

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.

Uploaded by

Shoaib Quraishi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Android WebView example

By mkyong (http://www.mkyong.com/author/mkyong/) | February 23, 2012 | Updated : August 29,


2012 | Viewed : 499,785 times

Androids WebView (http://developer.android.com/reference/android/webkit/WebView.html) 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
Markup

<?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


Markup

<?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"
/>

Rs. 2,090

Rs. 1,313

Rs. 1,434

Rs. 954

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

File : MainActivity.java
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
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");
}
}

straight fit rose print long skirt

Rs. 1,990
SHOP NOW

3. Android Manifest
WebView required INTERNET permission, add below into AndroidManifest.xml.
Markup

<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

Java

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 :
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");
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.

Download Source Code


Download it Android-WebView-Example.zip (http://www.mkyong.com/wpcontent/uploads/2012/02/Android-WebView-Example.zip) (16 KB)

References
1. Official Android webView example
(http://developer.android.com/resources/tutorials/views/hello-webview.html)
2. Android WebView Javadoc
(http://developer.android.com/reference/android/webkit/WebView.html)
3. Switching Android activity (http://www.mkyong.com/android/android-activity-from-one-screento-another-screen/)
Tags : android (http://www.mkyong.com/tag/android/)
webview (http://www.mkyong.com/tag/webview/)

Share this article on


Twitter (https://twitter.com/intent/tweet?text=Android WebView
example&url=http://www.mkyong.com/android/android-webviewexample/&via=mkyong)
Facebook (https://www.facebook.com/sharer/sharer.php?
u=http://www.mkyong.com/android/android-webview-example/)
Google+
(https://plus.google.com/share?url=http://www.mkyong.com/android/android-webview-example/)

40%
OFF

DRESS
Rs. 2,390

Rs. 1,434

Android ListView
example

Android activity from


one screen to another
s...

Android prompt user


input dialog example

Android spinner (drop


down list) example

SHOP NOW

Android alert dialog


example

Top 8 Java People You


Should Know

Android GridView
example

Android date picker


example

How to send SMS


message in Android

Android TabLayout
example

Reader also read :

Android Tutorial
How to send Email in
How to send SMS
Android : how to
(http://www.mkyong.com/tutorials/androidAndroid
message in Android
check if device has
tutorial/)
(http://www.mkyong.com/android/how(http://www.mkyong.com/android/howcamera
to-send-email-into-send-sms(http://www.mkyong.com/android/androidandroid/)
message-in-android/)
how-to-check-ifdevice-has-camera/)

How to turn on/off


camera LED /
flashlight in Android
(http://www.mkyong.com/android/howto-turn-onoff-cameraledflashlight-inandroid/)

About the Author

mkyong
Founder of Mkyong.com (http://mkyong.com) and HostingCompass.com
(http://hostingcompass.com), love Java and open source stuff. Follow him on
Twitter (https://twitter.com/mkyong), or befriend him on Facebook
(http://www.facebook.com/java.tutorial) or Google Plus
(https://plus.google.com/110948163568945735692?rel=author). If you like my tutorials,
consider make a donation to these charities (http://www.mkyong.com/blog/donate-tocharity/).

Comments
83 Comments Mkyong.com
Recommend 1

Share

Login

Sort by Best

Join the discussion

roy
4 years ago

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

10-30 18:53:06.966: W/webcore(20239): java.lang.Throwable: EventHub.removeMessages(int what = 107) is not supported before th
10-30 18:53:06.966: W/webcore(20239):

at android.webkit.WebViewCore$EventHub.removeMessages(WebViewCore.java:1671)

10-30 18:53:06.966: W/webcore(20239):

at android.webkit.WebViewCore$EventHub.access$7800(WebViewCore.java:920)

10-30 18:53:06.966: W/webcore(20239):

at android.webkit.WebViewCore.removeMessages(WebViewCore.java:1783)

10-30 18:53:06.966: W/webcore(20239):

at android.webkit.WebViewCore.removeMessages(WebViewCore.java:1783)

10-30 18:53:06.966: W/webcore(20239):

at android.webkit.WebView.sendOurVisibleRect(WebView.java:2858)

10-30 18:53:06.966: W/webcore(20239):

at android.webkit.ZoomManager.setZoomScale(ZoomManager.java:586)

10-30 18:53:06.966: W/webcore(20239):

at android.webkit.ZoomManager.access$1700(ZoomManager.java:49)

10-30 18:53:06.966: W/webcore(20239):

at android.webkit.ZoomManager$PostScale.run(ZoomManager.java:977)

10-30 18:53:06.966: W/webcore(20239):

at android.os.Handler.handleCallback(Handler.java:605)

10-30 18:53:06.966: W/webcore(20239):

at android.os.Handler.dispatchMessage(Handler.java:92)

10-30 18:53:06.966: W/webcore(20239):

at android.os.Looper.loop(Looper.java:137)

10-30 18:53:06.966: W/webcore(20239):

at android.app.ActivityThread.main(ActivityThread.java:4340)

10-30 18:53:06.966: W/webcore(20239):

at java.lang.reflect.Method.invokeNative(Native Method)

10-30 18:53:06.966: W/webcore(20239):

at java.lang.reflect.Method.invoke(Method.java:511)

10-30 18:53:06.966: W/webcore(20239):

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

10-30 18:53:06.966: W/webcore(20239):

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

10-30 18:53:06.966: W/webcore(20239):

at dalvik.system.NativeStart.main(Native Method)

Reply

shariq
2 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());

Reply

Nithish Kolli > shariq


9 months ago

Thanks for the info

Reply

certee > shariq


2 years ago

2 years ago

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

Reply

From > shariq


2 years ago

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

Reply

DJW
2 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?

Reply

Kira00 > DJW


2 years ago

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

Reply

sai
3 years ago

Hi ALL,
How can i pass a varibale to the URL, at the moment i am calling my html code but i also want to pass a
value to it.
thanks
sai

Reply

Reply

Omar
5 months ago

hey mkyong,
first of all a big thank you made my day with these android tutorial,
secondly you have a small mistake,
please change the name of
File : res/layout/main.xml WebView example
to be
File : res/layout/webview.xml WebView example
im i right.?

Reply

jgcoroleu
2 years ago

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

Reply

Sanmoy Ray
2 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/chro...
2. https://code.google.com/p/chro...

slow css transformation :


https://code.google.com/p/chro...
other issues :
https://code.google.com/p/andr... (text wrap bug)
https://code.google.com/p/andr... (broken filechooser)
The problems are so severe that developers are crying foul and asking the old webview back
https://code.google.com/p/andr...

Reply

ThePCWizard
4 years ago

thanks, I was looking for that second method.

Reply

Karsho
5 months ago

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

Reply

Half Moon
9 months ago

Find more webview tutorial here

Reply

Philip Jeffrey Trowe


a year ago

Why not take a look at my blog about how to create an Android app that displays an Image in an ImageView
control of the main Activity at the full width of the screen.
The app uses the following Android SDK objects:

The app uses the following Android SDK objects:


. ImageView
. LinearLayout
. Bitmap
. Activity
. XML layout
. LayoutParams
. Display
Also:
. layout_width
. layout_height
. id
. orientation
. vertical
. match_parent
XML attributes and values are covered.
Click the link BELOW! to see
http://androidprogrammeringcor...

Reply

a year ago

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

Reply

Nabaasa Gongo
a year ago

Thanks for the code.......it helped!

Reply

Murtaza Panhwar
a year ago

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

Reply

Corent Sudibyo
a year 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

Reply

manoj

a year 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...?

Reply

Load more comments

ALSO ON MKYONG.COM

Jackson 2 Convert Java Object to / from


JSON
3 comments 8 months ago

Rich Striker You do not show converting a List

of OBJECTS to JSON....fail.

Java 8 Streams filter examples


1 comment 4 months ago

Mykola Bova Thanks a lot!! A question.

Regarding parts Streams filter(), findAny() and


orElse() What if more than one result will be

Maven webxml attribute is required

Maven Deploy web application to WildFly

4 comments 5 months ago

1 comment 5 months ago

mkyong wow, thanks for your input, I didn't

Carol Thanks for this article, very explanatory

know about that!

and easy to understand

know about that!

Subscribe

Add Disqus to your site Add Disqus Add

and easy to understand

Privacy

40%
OFF

Rs. 1,434

40%
OFF

Rs. 954

40%
OFF

Rs. 1,134

40%
OFF

Rs. 1,434

Rs. 1,882
40%
OFF

Rs. 1,990

Rs. 1,890
40%
OFF

Rs. 1,434

Rs. 1,313

Rs. 1,990

Rs. 2,404

40%
OFF

Rs. 954

40%
OFF

Rs. 1,494

30%
OFF

40%
OFF

Rs. 2,090

Rs. 1,374

Rs. 2,289

Mkyong
50,113 likes

Like Page

Be the first of your friends to like this

Sign Up

40%
OFF

40%
OFF

Rs. 1,494
40%
OFF

Rs. 1,313
40%
OFF

Rs. 1,434
30%
OFF

Rs. 954
40%
OFF

Rs. 1,882

Rs. 1,134

Rs. 2,289

Rs. 2,404

Java 8 in Action: Lambdas, Streams, and functional-st (http://aax-us-east.amazonHead First Design Patterns (http://aax-us-east.amazonadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://www.amazon.com/Javaadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://www.am
Action-Lambdas-functional-style-programming/dp/1617291994/ref=sm_n_ma_dka_AP_pr_pub_0_0?
First-Design-Patterns-Freeman/dp/0596007124/ref=sm_n_ma_dka_AP_pr_pub_0_1?
imprToken=XLX4R5xvmDMDBueNbhKfVg&linkCode=w43&tag=mkyong-recommend05imprToken=XLX4R5xvmDMDBueNbhKfVg&linkCode=w43&tag=mkyong-recommend0520&linkId=b88b226952cbdeb5ea8ab51ed14dcb52)
20&linkId=b88b226952cbdeb5ea8ab51ed14dcb52)
(67)
(511)
$46.48 $49.99
$44.95 $59.99

Effective Java (2nd Edition) (http://aax-us-east.amazonClean Code: A Handbook of Agile Software Craftsma (http://aax-us-east.amazonadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://www.amazon.com/Effectiveadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://www.am
Java-2nd-Joshua-Bloch/dp/0321356683/ref=sm_n_ma_dka_AP_pr_pub_1_0?
Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=sm_n_ma_dka_AP_pr_pub_1_1?
imprToken=XLX4R5xvmDMDBueNbhKfVg&linkCode=w43&tag=mkyong-recommend05imprToken=XLX4R5xvmDMDBueNbhKfVg&linkCode=w43&tag=mkyong-recommend0520&linkId=b88b226952cbdeb5ea8ab51ed14dcb52)
20&linkId=b88b226952cbdeb5ea8ab51ed14dcb52)
(211)
(293)
$32.91 $54.99
$33.64 $49.99

Ads by Amazon (http://aax-us-east.amazon-adsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazonadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazon-adsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazonadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazon-adsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazonadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazon-adsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazonadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazon-adsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazonadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://aax-us-east.amazonadsystem.com/x/c/Qu7kOfKkRl_pAnpnLpThB3QAAAFVW8vE8wEAAAFKAd4PzsQ/http://www.amazon.com/ref=sm_n_ma_dka_AP_logo?imprToken=XLX4R5xvmDMDBueNbhKfVg&linkCode=w43&tag=mkyong-recommend0520&linkId=b88b226952cbdeb5ea8ab51ed14dcb52)

Favorites Links

Android Getting Started (http://developer.android.com/training/index.html)


Google App Engine Java (https://cloud.google.com/appengine/docs/java/)
Spring 2.5.x Documentation (http://docs.spring.io/spring/docs/2.5.x/reference/index.html)
Spring 3.2.x Documentation (http://docs.spring.io/spring/docs/3.2.x/spring-frameworkreference/html/)
Spring 4.1.x Documentation (http://docs.spring.io/spring/docs/4.1.x/spring-frameworkreference/html/)
Java EE 5 Tutorial (http://docs.oracle.com/javaee/5/tutorial/doc/docinfo.html)
Java EE 6 Tutorial (http://docs.oracle.com/javaee/6/tutorial/doc/docinfo.html)
Java EE 7 Tutorial (https://docs.oracle.com/javaee/7/tutorial/index.html)
Java 6 API (http://docs.oracle.com/javase/6/docs/api/overview-summary.html)
Java 7 API (http://docs.oracle.com/javase/7/docs/api/overview-summary.html)
Java 8 API (http://docs.oracle.com/javase/8/docs/api/overview-summary.html)
JSF Home Page (https://javaserverfaces.java.net/)
JSP Home Page (https://jsp.java.net/)
Maven Central Repository (http://search.maven.org/)
Hibernate ORM (http://hibernate.org/orm/)
JAX-WS Home Page (https://jax-ws.java.net/)
JAX-RS Home Page (Jersey) (https://jax-ws.java.net/)

Partners & Bookmarks


Java Code Geeks (http://www.javacodegeeks.com/)
TestNG Founder (http://beust.com/weblog/)
DZone (https://dzone.com)

About Mkyong.com
Mkyong.com is for Java and J2EE developers, all examples are simple and easy to understand, and
well tested in my development environment.

Mkyong.com is created, written by, and maintained by Yong Mook Kim, aka Mkyong. It is built on
WordPress (https://wordpress.org/), hosted by Liquid Web (//mkyong.com/go/liquidweb/), and the
caches are served by CloudFlare CDN.

Copyright 2008-2016 Mkyong.com, all rights reserved. Privacy Policy (/privacy-policy/)

You might also like