Make Android application of blog part 2: Android Web View


WebView is used to show web content in an activity of an Android application. There are two options either you forward the user to a browser for web content using Intent or show web content in the same application. In this tutorial, we will show how to display web content on the same application.

In the first part of this series, we learn how to create Android Splash screen.

1) Add <WebView> element in XML layout.

 <WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

So your activity_main.xml will look like this.
<?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.praval.androidlearninghub.MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

2) Get the reference of Web View and give URL to load.

In MainActivity.java add this to get the reference of WebView in the java class.

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

Now use loadUrl(String url) method to load URL in the WebView.

myWebView.loadUrl("https://www.androidlearninghub.com");

Now there is a catch here. URL will be loaded here but if you click anywhere in the WebView screen it will redirect you directly to your default browser. To fix this just set WebViewClient in your WebView object before loading any URL.

myWebView.setWebViewClient(new WebViewClient());

By default, javascript is not enabled here so you can enable javascript using WebSettings class and use its method setJavaScriptEnabled(Boolean)

WebSettings webSettings = myWebView.getSettings();

webSettings.setJavaScriptEnabled(true);

You can use WebSettings class to enable other settings also. Below are some examples.
  • setAllowContentAccess
  • setAllowFileAccess
  • setDisplayZoomControls
  • setCacheMode
  • setDatabaseEnabled
  • setGeoLocationEnabled
  • setLoadsImagesAutomatically
In the end, your MainActivity.java will look like this.
package com.praval.androidlearninghub;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

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);

        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        myWebView.setWebViewClient(new WebViewClient());

        myWebView.loadUrl("https://www.androidlearninghub.com");
    }
}

3) Give Internet access to the application using the Manifest file.

Now to load the website inside your application you need to give Internet access to your application using manifest file. Just add below permission in your file.
<uses-permission android:name="android.permission.INTERNET" />

Your Manifest file will look like this.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.praval.androidlearninghub">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
        </activity>
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

4) Run the application

android learning hub, androidlearninghub, android blog, android tutorial, webview, praval sharma, android webview, blogger, run android on real device, androidlearninghub.com

You will find below links helpful-

Comments