How to convert website to mobile app with android studio

To convert a website to a mobile app with Android Studio, you can use a WebView component to load the website within the app. Here are the basic steps:

  1. styles.xml = res // values // double click on  styles.xml or theme styal
  2. <item name="windowActionBar">false</item>
    

     

  3. Open Android Studio and create a new project.
  4. In the project structure, navigate to the “res” folder and create a new layout resource file called
  5. “activity_main.xml“. In this file, add a WebView component:
  6. delete all <TextView 

past ths code

<?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">

    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Add a ProgressBar to show the loading progress -->
    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />

</RelativeLayout>

2. “MainActivity.java past ths code

package com.blogstour.keenbech;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private ProgressDialog progressDialog;

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

        getSupportActionBar().hide();

        webView = findViewById(R.id.webView);

        // Configure WebView settings
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setWebViewClient(new MyWebViewClient());

        // Load the initial URL
        webView.loadUrl("https://www.keenbech.com/");
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to exit?");
            builder.setCancelable(false);
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }

    // Define the custom WebViewClient
    private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // Show the loading dialog
            progressDialog = ProgressDialog.show(MainActivity.this, "", "Loading...", true);

            // Load the URL in the WebView
            view.loadUrl(url);

            // Return true to indicate that we're handling the URL loading
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // Dismiss the loading dialog
            if (progressDialog != null) {
                progressDialog.dismiss();
            }
        }
    }
}

3. AndroidManifest.xml

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

4.

Leave a Comment

Your email address will not be published. Required fields are marked *