Create Web Browser Android Application Using Android Studio

Introduction

Android is one of the most popular operating systems for mobiles. In this article, I will show you how to create a Web Browser Android application using Android Studio.

Requirements

Steps to be followed

Follow these steps to create a web Browser Android application using Android Studio. I have included the source code above.

Step 1

Open Android Studio and start a New Android Studio Project.

Android Studio

Step 2

You can choose your application name and choose where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Android Studio

Now, select the version of Android and select the target Android devices.

Android Studio

Step 3

Now, add the activity and click "Next" button.

Android Studio

Add activity name and click "Finish".

Android Studio

Step 4

Go to activity_main.xml, This XML file contains the designing code for the app.

Android Studio

The XML code is given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="abu.webview.MainActivity">  
  8.   
  9.     <android.support.v4.widget.SwipeRefreshLayout  
  10.         android:id="@+id/swipe"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="match_parent">  
  13.   
  14.         <WebView  
  15.             android:id="@+id/webView"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="match_parent"/>  
  18.   
  19.     </android.support.v4.widget.SwipeRefreshLayout>  
  20.   
  21. </android.support.constraint.ConstraintLayout>  

Step 5

We need to make network requests. So, add internet permissions in AndroidManifest.xml file.

Android Studio

The AndroidManifest.xml code is given below.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="abu.webview">  
  4.     <uses-permission android:name="android.permission.INTERNET"/>  
  5.     <application  
  6.         android:allowBackup="true"  
  7.         android:icon="@mipmap/ic_launcher"  
  8.         android:label="@string/app_name"  
  9.         android:roundIcon="@mipmap/ic_launcher_round"  
  10.         android:supportsRtl="true"  
  11.         android:theme="@style/AppTheme">  
  12.         <activity android:name=".MainActivity">  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.MAIN" />  
  15.   
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.     </application>  

Step 6

Go to app, right click “Directory path”, then click on the path and add a new folder named Assets in the main folder. The below template shows how to add errorfile. I have attached an error file.

Android Studio

Android Studio

Step 7

Go to Main Activity.java. This Java program is the back-end language for Android.

Android Studio

The Java code is given below.

  1. package abu.webview;  
  2.   
  3. import android.support.v4.widget.SwipeRefreshLayout;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.webkit.WebView;  
  7. import android.webkit.WebViewClient;  
  8.   
  9. public class MainActivity extends AppCompatActivity {  
  10.   
  11.     WebView webView;  
  12.     SwipeRefreshLayout swipe;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);  
  20.         swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
  21.             @Override  
  22.             public void onRefresh() {  
  23.                 WebAction();  
  24.             }  
  25.         });  
  26.   
  27.         WebAction();  
  28.   
  29.     }  
  30.   
  31.   
  32.     public void WebAction(){  
  33.   
  34.         webView = (WebView) findViewById(R.id.webView);  
  35.         webView.getSettings().setJavaScriptEnabled(true);  
  36.         webView.getSettings().setAppCacheEnabled(true);  
  37.         webView.loadUrl("https://www.google.com/");  
  38.         swipe.setRefreshing(true);  
  39.         webView.setWebViewClient(new WebViewClient(){  
  40.   
  41.             public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {  
  42.   
  43.                 webView.loadUrl("file:///android_assets/error.html");  
  44.   
  45.             }  
  46.   
  47.             public void onPageFinished(WebView view, String url) {  
  48.                 // do your stuff here  
  49.                 swipe.setRefreshing(false);  
  50.             }  
  51.   
  52.         });  
  53.   
  54.     }  
  55.   
  56.   
  57.     @Override  
  58.     public void onBackPressed(){  
  59.   
  60.         if (webView.canGoBack()){  
  61.             webView.goBack();  
  62.         }else {  
  63.             finish();  
  64.         }  
  65.     }  
  66. }  

Step 8

Now, either go to menu bar and click "Make project" or press ctrl+f9 to debug the error.

Android Studio

Step 9

Then, click "Run" button or press shift+f10 to run the project. Select the "virtual machine" option and click OK.

Android Studio

Conclusion

We have successfully created a Web Browser Android application using Android Studio.

Android Studio

Android Studio

Android Studio

Up Next
    Ebook Download
    View all
    Learn
    View all