Code Example

Android WebView Snippets

Learn about WebView in android via simple code snippets and examples.

1. Android Web Explorer Example

A simple step by step Android browser app built using WebView code example.

This example will teach you the following:

  1. How to use WebView to build a Web Browser app.

This example will comprise the following files:

  • Browser1.java
  • viewclient.java

Step 1: Create Project

  1. Open your AndroidStudio IDE.
  2. Go to File-->New-->Project to create a new project.

Step 2: Dependencies

No External or special dependency is needed for this project.

Step 3: Design Layouts

Here are our layouts:

*(a). activity_browser1.xml

Create a file named activity_browser1.xml and design it as follows:

Add a WebView, an EditText and several buttons as shown below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_browser1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hp.exploretheweb.Browser1">

    <LinearLayout
        android:id="@+id/l2"
        android:weightSum="1.0"
        android:orientation="horizontal"
        android:layout_weight="0.10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText
            android:hint="Enter the Url"
            android:id="@+id/edittext"
            android:layout_width="234dp"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/buttonGo"
            android:text="GO"
            android:layout_width="66dp"
            android:layout_height="wrap_content" />

        <Button
            android:text="MIC"
            android:layout_width="66dp"
            android:layout_height="wrap_content"
            android:id="@+id/buttonMic" />

    </LinearLayout>
    <WebView
        android:layout_weight="0.8"
        android:id="@+id/web1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </WebView>
    <LinearLayout
        android:id="@+id/l3"
        android:weightSum="1.0"
        android:layout_weight="0.1"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:text="Forward"
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:text="Backward"
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:text="Reload"
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:text="Clear"
            android:id="@+id/button3"
            android:layout_width="100dp"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

Step 4: Write Code

Write Code as follows:

(a). Browser1.java

Create a file named Browser1.java

Here is the full code

package com.example.hp.exploretheweb;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Locale;

public class Browser1 extends AppCompatActivity implements View.OnClickListener {
    WebView wv;
    Button go,fwd,back,rel,clear,mic;
    EditText et;
    final int REQ_CODE_SPEECH_OUTPUT=143;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser1);
        wv = (WebView) findViewById(R.id.web1);
        go = (Button) findViewById(R.id.buttonGo);
        fwd = (Button) findViewById(R.id.button);
        back = (Button) findViewById(R.id.button1);
        rel = (Button) findViewById(R.id.button2);
        clear = (Button) findViewById(R.id.button3);
        et = (EditText) findViewById(R.id.edittext);
        mic = (Button) findViewById(R.id.buttonMic);
        mic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btntoopen();
            }
        });

        go.setOnClickListener(this);
        fwd.setOnClickListener(this);
        back.setOnClickListener(this);
        rel.setOnClickListener(this);
        clear.setOnClickListener(this);
        wv.setWebViewClient(new viewclient()); //for opening the url in the same website
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true); //for youtube access
    }
    private void btntoopen(){
        Intent intent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.EXTRA_LANGUAGE_MODEL);

        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, Locale.getDefault());
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,"Hii speak now.....");
        try{
            startActivityForResult(intent,REQ_CODE_SPEECH_OUTPUT);
        }
        catch (ActivityNotFoundException tim){
            Toast.makeText(this,"Mic Not Opened",Toast.LENGTH_SHORT).show();
        }

    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case REQ_CODE_SPEECH_OUTPUT:{
                if(resultCode==RESULT_OK&& null!=data){
                    ArrayList<String> voiceInText =data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                    et.setText(voiceInText.get(0));
                }
                break;
            }

        }
    }

    @Override
    public void onClick(View view) {
        if(view==go){
            String editextvalue=et.getText().toString();
            if(!editextvalue.startsWith("http://"))
                editextvalue= "http://" + "www."+ editextvalue;
            String url=editextvalue;
            wv.loadUrl(url);
            //hide keyboard after go button pressed
            InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(et.getWindowToken(),0);

        }
        if(view==fwd){
            if(wv.canGoForward())
                wv.goForward();
        }
        if(view==back){
            if(wv.canGoBack())
                wv.goBack();
        }
        if(view==rel){
            wv.reload();

        }
        if(view==clear)
            wv.clearHistory();

    }
}

(b). viewclient.java

Create a file named viewclient.java

Here is the full code

package com.example.hp.exploretheweb;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class viewclient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return  true;
    }
}

Run

Simply copy the source code into your Android Project,Build and Run.

Read More.