Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

WebView


Android WebView is used to display web page in android. The web page can be loaded from same application or URL. It is used to display online content in android activity.

Android WebView uses webkit engine to display web page.

The android.webkit.WebView is the subclass of AbsoluteLayout class.

The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.

 

Let’s see the simple code to display Kreationnext.com web page using web view.

  1. WebView mywebview = (WebView) findViewById(R.id.webView1);
  2. mywebview.loadUrl(“http://www.Kreationnext.com/”);

Let’s see the simple code to display HTML web page using web view. In this case, html file must be located inside the asset directory.

  1. WebView mywebview = (WebView) findViewById(R.id.webView1);
  2. mywebview.loadUrl(“file:///android_asset/myresource.html”);

Let’s see another code to display HTML code of a string.

  1. String data = “<html><body><h1>Hello, Kreationnext!</h1></body></html>”;
  2. mywebview.loadData(data, “text/html”“UTF-8”);

Android WebView Example

Let’s see a simple example of android webview.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     tools:context=“.MainActivity” >
  6.     <WebView
  7.         android:id=“@+id/webView1”
  8.         android:layout_width=“match_parent”
  9.         android:layout_height=“match_parent”
  10.         android:layout_alignParentTop=“true”
  11.         android:layout_centerHorizontal=“true”
  12.         android:layout_marginTop=“42dp” />
  13. </RelativeLayout>

Activity class

File: MainActivity.java
  1. package com.example.webview;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.webkit.WebView;
  6. public class MainActivity extends Activity {
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.         WebView mywebview = (WebView) findViewById(R.id.webView1);
  12.          //mywebview.loadUrl(“http://www.Kreationnext.com/”);
  13.         /*String data = “<html><body><h1>Hello, Kreationnext!</h1></body></html>”;
  14.         mywebview.loadData(data, “text/html”, “UTF-8”); */
  15.         mywebview.loadUrl(“file:///android_asset/myresource.html”);
  16.     }
  17.     @Override
  18.     public boolean onCreateOptionsMenu(Menu menu) {
  19.         // Inflate the menu; this adds items to the action bar if it is present.
  20.         getMenuInflater().inflate(R.menu.activity_main, menu);
  21.         return true;
  22.     }
  23. }

Output:

Let’s see the output if you load the HTML page.

android-webview-output1

Let’s see the output if you load the Kreationnext.com web page.

android-webview-output2

 

Comments

comments