Android WebView Example PDF
Android WebView Example PDF
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");
}
}
Rs. 1,990
SHOP NOW
3. Android Manifest
WebView required INTERNET permission, add below into AndroidManifest.xml.
Markup
4. Demo
Java
4. Demo
By default, just display a button.
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");
}
}
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/)
40%
OFF
DRESS
Rs. 2,390
Rs. 1,434
Android ListView
example
SHOP NOW
Android GridView
example
Android TabLayout
example
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/)
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
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)
at android.webkit.WebViewCore$EventHub.access$7800(WebViewCore.java:920)
at android.webkit.WebViewCore.removeMessages(WebViewCore.java:1783)
at android.webkit.WebViewCore.removeMessages(WebViewCore.java:1783)
at android.webkit.WebView.sendOurVisibleRect(WebView.java:2858)
at android.webkit.ZoomManager.setZoomScale(ZoomManager.java:586)
at android.webkit.ZoomManager.access$1700(ZoomManager.java:49)
at android.webkit.ZoomManager$PostScale.run(ZoomManager.java:977)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
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
Reply
2 years ago
Yes! The code would try to run the website in the device's internet browser without this code. Thanks!
Reply
Right shariq ! I've done same thing to make the webview able to navigate properly !
Reply
DJW
2 years ago
Reply
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...
Reply
ThePCWizard
4 years ago
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
Reply
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:
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
Reply
Murtaza Panhwar
a year ago
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
ALSO ON MKYONG.COM
of OBJECTS to JSON....fail.
Subscribe
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
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
Favorites Links
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.