This example shows basic implementation of WebView component in Android using Java.
Android WebView is an extension of View class that allows you to display web pages as a part of your activity layout. If you want to build a web application or just a web page as a part of a client application, you can do it using WebView. This example shows basic implementation of android webview.
AndroidManifest.xml
1 2 3 4 |
<manifest ... > <uses-permission android:name="android.permission.INTERNET" /> ... </manifest> |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".activity.MainActivity"> <WebView android:id="@+id/webview" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView) findViewById(R.id.webview); // Load web page from android asset file path webView.loadUrl("file:///android_asset/index.html"); // Load web page from external URL // webView.loadUrl("http://www.example.com"); } } |