Android WebView example
来源:互联网 发布:迅龙手机数据恢复软件 编辑:程序博客网 时间:2024/06/14 19:02
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 theWebView
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 intoAndroidManifest.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.
![android-webview-example android webview example](http://www.mkyong.com/wp-content/uploads/2012/02/android-webview-example.png)
Click on button, WebView is display.
![android-webview-example-result android webview example](http://www.mkyong.com/wp-content/uploads/2012/02/android-webview-example-result.png)
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.
![android-webview-example-result-2 android webview example](http://www.mkyong.com/wp-content/uploads/2012/02/android-webview-example-result-2.png)
Download Source Code
- Android WebView example
- Android WebView example
- webview example
- 【Android】【Webview】WebView 简介
- Android HorizontalScrollview Example
- Android Image Rotate Example
- Android Parcelable Example
- Android Button example
- Android EditText example
- Android EditText password example
- Android CheckBox example
- Android RadioButton example
- Android ToggleButton example
- Android RatingBar example
- Android Spinner example
- Android DatePicker example
- Android TimePicker example
- Android ProgressBar example
- Android左右滑屏的实现
- select * from 与select count(*) from 的区别
- hdu 1231 最大连续子序列
- WebView(二)
- 吉他第九课
- Android WebView example
- Slow Virtual Hosts in OS X Lion
- android移动广告平台集成的一些问题
- select count(*)和select count(1)的区别
- HDU3723 Delta Wave
- 黑马程序员--转义符和类型转换
- 转:五步快速使用LINQPad尝鲜StreamInsight
- javaScript 框架函数互相调用
- 请教:java中的tag使用方法,如何设置呢?完全不知道怎么把它写入的,就见一大段嵌入的标签!晕