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 the "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 the "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. <android.support.v4.widget.SwipeRefreshLayout
  9. android:id="@+id/swipe"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent">
  12. <WebView
  13. android:id="@+id/webView"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"/>
  16. </android.support.v4.widget.SwipeRefreshLayout>
  17. </android.support.constraint.ConstraintLayout>
Step 5
We need to make network requests. So, add internet permissions in the 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. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. </application>
Step 6
Go to the 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 an error file. 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. import android.support.v4.widget.SwipeRefreshLayout;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.webkit.WebView;
  6. import android.webkit.WebViewClient;
  7. public class MainActivity extends AppCompatActivity {
  8. WebView webView;
  9. SwipeRefreshLayout swipe;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);
  15. swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  16. @Override
  17. public void onRefresh() {
  18. WebAction();
  19. }
  20. });
  21. WebAction();
  22. }
  23. public void WebAction(){
  24. webView = (WebView) findViewById(R.id.webView);
  25. webView.getSettings().setJavaScriptEnabled(true);
  26. webView.getSettings().setAppCacheEnabled(true);
  27. webView.loadUrl("https://www.google.com/");
  28. swipe.setRefreshing(true);
  29. webView.setWebViewClient(new WebViewClient(){
  30. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  31. webView.loadUrl("file:///android_assets/error.html");
  32. }
  33. public void onPageFinished(WebView view, String url) {
  34. // do your stuff here
  35. swipe.setRefreshing(false);
  36. }
  37. });
  38. }
  39. @Override
  40. public void onBackPressed(){
  41. if (webView.canGoBack()){
  42. webView.goBack();
  43. }else {
  44. finish();
  45. }
  46. }
  47. }
Step 8
Now, either go to the menu bar and click "Make a project" or press ctrl+f9 to debug the error.
Android Studio
Step 9
Then, click the "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