Monday, March 15, 2021

WebView in Android

Android WebView Example

android web view

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.



Android WebView Example

Let's see the simple code to display javatpoint.com web page using web view.


  1. WebView mywebview = (WebView) findViewById(R.id.webView1);  
  2. mywebview.loadUrl("http://www.javatpoint.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, Javatpoint!</h1></body></html>";  
  2. mywebview.loadData(data, "text/html""UTF-8");  

Complete Android WebView Example

Let's see a complete example of Android WebView.

activity_main.xml

File: activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.webview.MainActivity">  
  8.   
  9.     <WebView  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:id="@+id/webView"  
  13.         app:layout_constraintBottom_toBottomOf="parent"  
  14.         app:layout_constraintLeft_toLeftOf="parent"  
  15.         app:layout_constraintRight_toRightOf="parent"  
  16.         app:layout_constraintTop_toTopOf="parent" />  
  17.   
  18. </android.support.constraint.ConstraintLayout>  

To add the web page (.html, .jsp) locally in application, they are required to place in the assets folder. An assets folder is created as: right click on app -> New -> Folder -> Assets Folder ->main or simply create an assets directory inside main directory.

Activity class

File: MainActivity.java
  1. package example.javatpoint.com.webview;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.webkit.WebView;  
  6.   
  7. public class MainActivity extends AppCompatActivity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.         WebView mywebview = (WebView) findViewById(R.id.webView);  
  14.         // mywebview.loadUrl("https://www.javatpoint.com/");  
  15.   
  16.         /*String data = "<html><body><h1>Hello, Javatpoint!</h1></body></html>"; 
  17.         mywebview.loadData(data, "text/html", "UTF-8"); */  
  18.   
  19.         mywebview.loadUrl("file:///android_asset/myresource.html");  
  20.     }  
  21. }  

Output:

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

android webview example output 1

Let's see the output if you load the javatpoint.com web page.

android webview example output 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile