Blog Details

img
Design

What is WebView in Android

Spoke Right / 20 Nov, 2023

WebVeiw in android

To display a web page in android loaded from the same application or URL, the WebView is used in Android. Thus, online content can be displayed in an Android activity using the Android WebView which displays a web page using the Webkit engine. In Android, the AbsoluteLayout class has a subclass android.webkit.WebView.

IFrame

To load and display a web page, the Android WebView class uses its loadUrl() and loadData() methods.

Android WebView Codes:

To display a web page using WebView:

WebView mywebview = (WebView) findViewById(R.id.webView1);  
mywebview.loadUrl("https://www.w3schools.blog/");

To display an HTML web page using WebView:

For this, we need to locate the HTML file inside the asset directory.

WebView mywebview = (WebView) findViewById(R.id.webView1);  
mywebview.loadUrl("file:///android_asset/myresource.html");

To display an HTML code of a string:

Hello World!!

String data = "
"; mywebview.loadData(data, "text/html", "UTF-8");

Android WebView Example:

activity_main.xml:



 
   
 

The web page (.html, .jsp) needs to be placed in the assets folder, to add it locally in an application. To create an assets folder follow the below steps:

 

right-click on app -> New -> Folder -> Assets Folder ->main

OR

Create an assets directory inside the main directory.

Activity class: (File: MainActivity.java)

package com.example.radioapp;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
 
public class MainActivity extends AppCompatActivity {
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       WebView mywebview = (WebView) findViewById(R.id.webView);
 
 
       mywebview.loadUrl("file:///android_asset/myresource.html");
   }
}

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0