WebView in Android

WebView is an android UI component that is used in different purpose like display a web page, online components, displaying HTML Content and apply HTML Formatting on your android app. 

Generally, it is used to display web page in android. And android web page can be loaded from same application or project. For displaying a webpage in android webview uses webkit engine. And for loading data, android provided two methods loadUrl() and loadData() to display a content on  webpage. These are some uses of  WebView-

1. Display online content-

     WebView myWebView = (WebView) findViewById(R.id.webView1);

      myWebView.loadUrl(“https://www.mySite.com”);

2. Display HTML Webpage -

    myWebView.loadUrl(locationOfHTMlFile);

3. Displaying HTML Formatting-

    String data =”<html><body><i><h1>WebView Example </h1></i></body></html>”;

    mywebview.loadData(data,”text/html”,”utf-8”);

Android WebView Example :

Provide internet permission on AndroidManifest.xml file.

<uses-permission android:name='android.permission.INTERNET' /> 


activity_main.xml 

<?xml version='1.0' encoding='utf-8'?>
<LinearLayout 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'
    android:orientation='vertical'
    tools:context='com.example.msclient009.webviewexample.MainActivity'>
    <WebView
        android:layout_height='50dp'
        android:layout_width='match_parent'
        android:id='@+id/webView1' />
    <WebView
        android:id='@+id/webView2'
        android:layout_width='match_parent'
        android:layout_height='435dp'
        android:layout_alignParentTop='true'
        android:layout_centerHorizontal='true'
        android:layout_marginTop='15dp'
        tools:layout_editor_absoluteX='0dp'
        tools:layout_editor_absoluteY='233dp' />
</LinearLayout>


ActivityMain.java

package com.example.msclient009.webviewexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView1=(WebView)findViewById(R.id.webView1);
        WebView webView2=(WebView)findViewById(R.id.webView2);
        String data = '<html><body><h1 style='align:center;color:red'><i>WebView Examle<i></h1><div style='height:5;width:100%;background:#00f;align:center;fontStyle:bold;color:white;'>Hello</div></body></html>';
        webView1.loadData(data, 'text/html', 'UTF-8');
        webView2.loadUrl('https://www.mindstick.com');
    }
}


Output :

WebView in Android



Last updated:9/7/2019 12:18:10 AM
Arti Mishra

Arti Mishra

Student

2 Comments

Leave Comment