If you want to build an app in simple then you can use WebView. Before you start installing Android Studio and start building an app, your website should be responsive and mobile-ready. You can take Google’s mobile-friendly test to check if your website is optimized for mobile devices or not. Once you have completed the test, you can follow the below steps.
Step 1: Get Ready With Android Studio
Install latest version of Android Studio. Create a new project in Android Studio and name it.
Step 2: Create App Interface And WebView Element
Open res -> layout -> activity_main.xml, create the application interface and add WebView element to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> |
Step 3: Open AndroidManifest.xml file and add internet permission to it just after the package name. It is required because the App will load data directly from the website.
1 |
<uses-permission android:name="android.permission.INTERNET"></uses-permission> |
src/main/AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mwcUKGCab_156708546"> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="com.mwcUKGCab_156708546.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Step 4: After adding the permissions the application is complete but when you run you will find that it will open the links in browser not in application itself. Solution for this is add this line of code in your MainActivity.java class.
1 |
mywebView.setWebViewClient(new WebViewClient()); |
Step 5: Now to add back buttons to the application to need to add following code to your MainActivity.java class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public void onBackPressed(){ if(mywebView.canGoBack()) { mywebView.goBack(); } else{ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure,You want to exit") .setNegativeButton( "No", null) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finishAffinity(); } }).show(); } } |
MainActivity.java complete code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package com.mwcUKGCab_156708546; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.Bitmap; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { private WebView mywebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mywebView=(WebView) findViewById(R.id.webview); mywebView.setWebViewClient(new WebViewClient()); mywebView.loadUrl("https://www.mywebcode.com/"); WebSettings webSettings=mywebView.getSettings(); webSettings.setJavaScriptEnabled(true); } public class mywebClient extends WebViewClient{ @Override public void onPageStarted(WebView view, String url, Bitmap favicon){ super.onPageStarted(view,url,favicon); } @Override public boolean shouldOverrideUrlLoading(WebView view,String url){ view.loadUrl(url); return true; } } @Override public void onBackPressed(){ if(mywebView.canGoBack()) { mywebView.goBack(); } else{ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure,You want to exit") .setNegativeButton( "No", null) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finishAffinity(); } }).show(); } } } |