Android Club – How to create Anroid Browser application?

In this tutorial, I will show how to create browser application for Android OS. This browser will open www.google.com by default. Moreover, we will add such function to this browser, which help open URL from another application. So let’s start! First of all, create new Android Application Project in Android Studio. By default, your project will have MainActivity. Open AndroidManifest.xml in manifests folder. Add new intent filter:

            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"/>
            </intent-filter>

Add new permission to connect to INTERNET.

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

After changing AndroidManifest.xml, it should look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="net.joerichard.joebrowser" >

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

    <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
        <activity
           android:name=".MainActivity"
           android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Now open your layout file – activity_main.xml. Add new WebView:

<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"
   tools:context=".MainActivity">

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

</RelativeLayout>

Now open MainActivity.java. Find button and enable JavaScript:

        final WebView wvBrowser = (WebView) findViewById(R.id.wvBrowser);
        // let's enable javascript in WebView
        wvBrowser.getSettings().setJavaScriptEnabled(true);

Next step is to set WebViewClient. You can ask why we need this client? I answer: this client helps us to handle opening new links. Without this, our browser will try to open link in another browser. In that case, what is the point to create browser application, if we open link in another browser.

        wvBrowser.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                wvBrowser.loadUrl(url);
                return true;
            }
        });

Next step is to check application opened from home screen or from another application. If intent’s data, which we are supposed to receive from another application, is null, it means that we opened from home screen. In this case we open www.google.com by default. If data is not null we can get URL. In this case we open this URL.

        Intent intent = getIntent();
        if(intent.getData()!=null)
        {
            String schema = intent.getData().getScheme();
            String host = intent.getData().getHost();
            String path = intent.getData().getPath();
            String url = schema + "://www."+host+path;
            wvBrowser.loadUrl(url);
        } else {
            // data is null
            // user opened application from home screen
            // let's open default url: google.com
            wvBrowser.loadUrl("http://www.google.com");
        }

When we get URL(http://joerichard.net/ac/android-club-android-presentations/) from another application, we get it in different paramenters. For example, schema is intent’s schema: http. host is domain of url: joerichard.net. path is value which comes after domain : /ac/android-club-android-presentations/

After all, MainActivity.java should look like this:

package net.joerichard.joebrowser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {

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

        final WebView wvBrowser = (WebView) findViewById(R.id.wvBrowser);
        // let's enable javascript in WebView
        wvBrowser.getSettings().setJavaScriptEnabled(true);

        wvBrowser.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                wvBrowser.loadUrl(url);
                return true;
            }
        });

        Intent intent = getIntent();
        if(intent.getData()!=null)
        {
            String schema = intent.getData().getScheme();
            String host = intent.getData().getHost();
            String path = intent.getData().getPath();
            String url = schema + "://www."+host+path;
            wvBrowser.loadUrl(url);
        } else {
            // data is null
            // user opened application from home screen
            // let's open default url: google.com
            wvBrowser.loadUrl("http://www.google.com");
        }

    }

}

Screenshot:
Screenshot_2015-07-11-10-55-11
Download source code here
Download apk here(for security reasons I could not upload apk, so I changed it to doc. Before you install it, change filetype from doc to apk)