Vous êtes sur la page 1sur 7

Android WebView example

http://www.mkyong.com/android/android-webview-example/

Java Forum

Advertise

Contact Us

RSS

Java Core

Web Frameworks

Spring

Hibernate

Web Service

Others...

Search

Android WebView example


Published: February 23, 2012 , Updated: February 23, 2012 , Author: mkyong

Androids 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.
Live Demo Try your AJAX code online! Demo -> ZK Explorer Zkoss.org Really Cool JSF DataTable Video Tutorials, Demos & Examples Free Download & more www.simplica.com Integrate Java and C++ Easy, professional solution for all your Java-C++ integration projects.
www.CodeMesh.com

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. Mkyong en Facebook
Me gusta A 2,328 personas les gusta Mkyong.

Sukhmeet

Satish

Sameh

Anand

Santhosh

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>

Mohammad

Hassan

Ramesh

Sneha

Mohamed

Plug-in social de Facebook

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 import import import import android.app.Activity; android.content.Context; android.content.Intent; android.os.Bundle; android.view.View;

1 de 7

27/02/12 17:03

Android WebView example


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() {

http://www.mkyong.com/android/android-webview-example/

@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"); } }

Subscribe Mkyong via email :

Subscribe

Latest Posts
Android WebView example

3. Android Manifest
WebView required INTERNET permission, add below into AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />

Android TabLayout example How to import class automatically in Eclipse How to remove unused imports in Eclipse Android GridView example How to display line numbers in Eclipse How to check if Date is within a certain range in Java? How to check if date is valid in Java Struts 2 dynamic image example try-with-resources example in JDK 7

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

Advertisement

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

As a market leader in the reseller hosting industry, micfo.com offers you a great opportunity to start your own web hosting company. Join them today.

Latest Comments
Navin on JSON example with Jersey + Jackson

2 de 7

27/02/12 17:03

Android WebView example


</activity> </application> </manifest>

http://www.mkyong.com/android/android-webview-example/

How to write a service consuming a JSON string as a parameter. I mean, method should consume directl... Meethas on Maven + (Spring + Hibernate) Annotation + MySql Example

4. Demo
By default, just display a button.

Hi , I need a simple hibernate project without maven or ant and i need to run it in apache tomcat se... HibLearner on Hibernate fetching strategies examples Suppose I have an Order table with 50 columns and I have an Order class with 50 properties and mappe... mkyong on Connect to PostgreSQL with JDBC driver Make sure postgresql-9.1-901.jdbc3.jar is configured correctly and able to locate in your class path... mkyong on Struts 2 Hello World Example Not really related with above article, please post your question on JavaNullPointer.com? And elabora...

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;

3 de 7

27/02/12 17:03

Android WebView example


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

http://www.mkyong.com/android/android-webview-example/

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 (16 KB)

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

4 de 7

27/02/12 17:03

Android WebView example

http://www.mkyong.com/android/android-webview-example/

To Whom It May Concern, If you have any Java questions or problems, please post at this new JavaNullPointer.com forum. Best Regards, mkyong

Related Posts
Android TabLayout example Android GridView example Android ListView example How to set default activity for Android application Attach Android source code to Eclipse IDE

Popular Posts
Top 5 free Java eBooks Top 8 Java people you should know Top 20 Java websites you must visit Top 10 Java regular expression examples Top 10 open source forums in collection Top 5 open source Q&A systems

Java / J2EE Tutorials

Leave a Reply
Name (required)

Mail (will not be published) (required)

Website

[Note] - To post source code in comment, wrap your source code like this :

1. Java - <pre lang="java"> Java codes here </pre> 2. XML - <pre lang="xml"> XML here </pre> 3. HTML - <pre lang="html4strict"> HTML here </pre>

5 de 7

27/02/12 17:03

Android WebView example

http://www.mkyong.com/android/android-webview-example/

Submit Comment
Notify me of followup comments via e-mail

Poll : What is your current Java web application framework?


Apache Struts Apache Struts2 Apache Tapestry Apache Wicket Custom in-house Framework Google Web Toolkit (GWT) JavaServer Faces (JSF 1.x) JavaServer Faces (JSF 2.x) JBoss Seam No framework, code in JSP Oracle Application Framework Others Spring MVC Spring WebFlow Stripes View Results

Vote

All Available Tutorials


Java Core Technologies : Java I/O, Java RegEx, Java XML, Java JSON, JDBC, Java Misc J2EE Frameworks : Hibernate, JSF 2.0, Spring Core, Spring MVC, Spring Security, Apache Wicket, Struts 1.x, Struts 2.x Web Service : JAX-WS (SOAP), JAX-RS (REST) Build Tools : Maven, Archiva Unit Test Frameworks : jUnit, TestNG Others... : jQuery, Java MongoDB

Favorites Links
DZone - Fresh Links Official Java EE 5 Tutorial Spring 2.5.x documentation Hibernate core documentation Java SE 6.0 API documentation Java EE 6.0 API documentation Java Secure Socket Extension (JSSE) Reference Guide JSP home page JSF home page Eclipse IDE for Java developer Maven home page Ant home page Struts 1.3 documentation Struts 2.2 documentation Maven central repository Java.Net Maven repository Martin Fowler

Friends & Links


Java Training JavaScript Training Java Web Hosting Java & Co. Tutorials PHP Tutorials TenthOfMarch Find Free Icons Web Security Blog

About Mkyong.com
Mkyong.com is about a person who is facing the big tree (Java web development), always wonder why the tree (Java) is so big!

All tutorials and examples are unique, and from my personal experience, if you find mistake in my tutorial, please correct me :) after all, we learned through the process. For Java question that is not related to the tutorial, you can post here - Java Q&A forum. 1. Twitter - Follow Me 2. Facebook - Fan Page 3. RSS - Subscribe It Advertise With Us

6 de 7

27/02/12 17:03

Android WebView example

http://www.mkyong.com/android/android-webview-example/

Copyright 2008-2011 Mkyong Enterprise, all rights reserved. Privacy Policy

7 de 7

27/02/12 17:03

Vous aimerez peut-être aussi