Friday 14 February 2020

Create Web Browser Android Application Using Android Studio

Introduction

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



Requirements

  • Android Studio model 2.3.3
  • Little bit XML and JAVA knowledge.
  • Android Emulator (or) Android mobile
  • Download link (Android Studio)
  • Steps to be followed

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



Step 1

Open Android Studio and start a New Android Studio Project.

Step 2

You can pick out your application call and pick out wherein your venture is stored. If you want to use C++ for coding the venture, mark the "Include C++ support", and click the "Next" button.
Now, pick the model of Android and choose the goal Android devices. Read more info at Android Training 
Step 3

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

Add pastime name and click on "Finish".

Step 4

Go to activity_main.Xml, This XML document consists of the designing code for the app.

The XML code is given under.
  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, upload net permissions in the AndroidManifest.Xml record.

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 the app, proper click “Directory route”, then click on on the route and add a brand new folder named “Assets” in the foremost folder. The beneath template shows the way to add an error file. I have attached an error record.

Step 7

Go to Main Activity.Java. This Java software is the back-quit language for Android.

The Java code is given underneath.
  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 visit the menu bar and click "Make challenge" or press ctrl+f9 to debug the error.
Step 9

Then, click on the "Run" button or press shift+f10 to run the undertaking. Select the "digital machine" alternative and click on OK.

Conclusion

We have efficaciously created a Web Browser Android application for the use of Android Studio.

If you want to learn more about Android, then The Complete Android App Development Course is a great course, to begin with



No comments:

Post a Comment