Home Android WebView Android Example

WebView Android Example

69
0
SHARE

Android’s WebView is for viewing web pages in your app. It transforms your app to a web application but provides no-browser like widgets.

In this tutorial, you need to create a single activity, enter the URL and the respective URL will be shown in your browsing window.

NOTE :  This project is developed in Android Studio, and tested in Android Marshmallow 6.0 .

To provide an application as a part of client’s app, we can do it by using Webview. The Webview class is basically an extension of View that make you allow display web pages on your activity layout. It does not need any navigation control or address bar like fully developed web browser. It’s job is to show you a web page. A scenario where it is mostly helpful is when you need something to get updated like user-guide or any agreement. So, what you need to do is just to create an Activity containing Webview and display your document hosted online.

Another use of webview can be that if you need the user to provide the data so the application requires internet connection to retrieve data, such as mail. In this case, you ability acquisition that it’s easier to body a WebView in your Android appliance that shows a web page with all the user data, rather than assuming a arrangement request, again parsing the abstracts and apprehension it in an Android layout. Instead, you can architecture a web page that’s tailored for Android accessories and again apparatus a WebView in your Android appliance that endless the web page.

1. Android Layout Files for WebView

To add a webview to your application, include the <Webview> element in your layout. In your .xml file whose path is ” res/layout/activity_web.xml ” , edit the following UI components or you can choose yourself.

File : activity_web.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.cloudinfinity.webview.WebActivity">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GO"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignTop="@+id/button"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/button" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close"
        android:id="@+id/button2"
        android:layout_below="@+id/button"
        android:layout_alignParentStart="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Refresh"
        android:id="@+id/button3"
        android:layout_alignTop="@+id/button2"
        android:layout_toEndOf="@+id/button2" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/button4"
        android:layout_alignTop="@+id/button3"
        android:layout_toEndOf="@+id/button3" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Forward"
        android:id="@+id/button5"
        android:layout_alignTop="@+id/button4"
        android:layout_toEndOf="@+id/button4" />

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_below="@+id/button2"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />
    </RelativeLayout>

 

2. Activity Code 

Here, is your activity file in which you can add control to your webview for opening any address you want to show up to your application,

File : WebActivity.java

package com.example.cloudinfinity.webview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class WebActivity extends AppCompatActivity {
    Button b1,b2,b3,b4,b5;
    WebView wv;
    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        b1=(Button)findViewById(R.id.button);
        b2=(Button)findViewById(R.id.button2);
        b3=(Button)findViewById(R.id.button3);
        b4=(Button)findViewById(R.id.button4);
        b5=(Button)findViewById(R.id.button5);

        wv=(WebView)findViewById(R.id.webView);
        et=(EditText)findViewById(R.id.editText);

        wv.setWebViewClient(new webCont());
        wv.loadUrl("http.//www.google.com");

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = et.getText().toString();
                wv.loadUrl("http://" + url);
            }
        });
    }
    class webCont extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

 

3. Android Manifest

WebView needed INTERNET permission, add code shown below into AndroidManifest.xml.

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

4. Run the application

Run the app, enter the URL in the edittext and click Go.

webview

LEAVE A REPLY

Please enter your comment!
Please enter your name here

five × five =