fastread homefastrread library fastread menu

Android : WebView

Tutorial by:      Date: 2016-05-10 05:04:08

❰ Previous Next ❱

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 fastread.aitechtonic.com web page using web view.

 
  1. WebView mywebview = (WebView) findViewById(R.id.webView1);  
  2. mywebview.loadUrl("http://www.fastread.aitechtonic.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, fastread.aitechtonic!</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.   
  7.     <WebView  
  8.         android:id="@+id/webView1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="42dp" />  
  14.   
  15. </RelativeLayout>  

Activity class

 

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

❰ Previous Next ❱