What is webview

Android WebView is used to display the web pages in android application.

There are two methods loadUrl() and loadData() of android WebView is responsible for load and display the web page in android application.


WebView:.

In this section i am going to show the WebView with practical example.

First open the android studio and create a project and give the name of your project like WebViewDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one WebView , one Button and one TextView. Open the activity_main.xml file and code look like this


<RelativeLayout 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"
    tools:context="com.example.niitpc.myapplication.MainActivity">


    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="11dp"
        android:layout_marginStart="11dp"
        android:layout_marginTop="12dp"
        android:text="Button" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignStart="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginTop="12dp"
        android:ems="10"
        android:inputType="text" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/editText"
        android:layout_alignStart="@+id/editText"
        android:layout_below="@+id/editText"
        android:layout_marginTop="16dp" />

</RelativeLayout>


       

Open the AndroidManifest.xml file and add one line

           <uses-permission android:name="android.permission.INTERNET">
      

This is the complete code of AndroidManifest.xml.

     <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.niitpc.myapplication">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>//This is the line you need to add for internet permission.
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

   WebView browser;
   EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        browser=(WebView)findViewById(R.id.webView);
        editText=(EditText)findViewById(R.id.editText);

    }
    public  void onClickButton(View v)
    {
        String url=editText.getText().toString();
        browser.loadUrl(url);

    }

}


       

Explanation:

WebView browser:This statement create the object browser of WebView.

TextView textView:This statement create the object textView of TextView.

browser=(WebView)findViewById(R.id.webView):This statement initialized the browser object.

editText=(EditText)findViewById(R.id.editText):This statement initialized the editText object.

After initialization i have to created the onClickButton().

String url=editText.getText().toString():The getText() return the enter url after converting it into String value assign into the url variable.

browser.loadUrl(url):The loadUrl(url) is responsible to display the web page in android app it will take the url of website here i passing the url of website inside the url variable .

You do not forget to set the android:onClick="onClickButton".If you do not set this property button will not perform any action .


WebView

What is webview for video Click here

Smiley face                     Smiley face