ComputerBasForum

Hier tips en tricks

Je bent niet ingelogd.

#1 2022-04-30 16:43:56

Bas
Administrator
Geregistreerd: 2019-11-10
Posts: 79

Convert any website to Android app using Android Studio

Step 1

Open Android Studio and click on New Project and select Empty Activity.

Step 2

Give your application name, folder location to store, and select JAVA language. Minimum SDK of Android OS. Click on Finish.

Step 3

In the project screen you will see two files are already opened. MainActivity.java and activity_main.xml. Open one more file from manifests from the left side menu. Expand manifests and double click on AndroidManifest.xml file. Select activity_main.xml and click on the code.

Step 4

Open activity_main.xml then copy and paste the below code as it is into your activity_main.xml file. This code will convert the Android App into Web View. Very important, replace from <Textview to parent” /> with this code:

<WebView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/webView"
          android:layout_alignParentTop="true"
          android:layout_alignParentLeft="true"
          android:layout_alignParentStart="true"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentEnd="true"
          tools:ignore="MissingConstraints" />

So the total code will look like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 5

Allow Internet connection to the Android App by adding code in AndroidManifest.xml file right under package="com.example.yourappname">

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

Step 6

Expand the res folder from the left side menu, expand values then expand themes and double click on both themes.xml files one by one, and find DarkActionBar and replace with NoActionBar. This code will remove the extra header.

Step 7

Now add the icon for your application. Copy your icon first then expand res right-click on drawable and paste the copied icon, you will find your icon name under drawable. In this example the icon name is “bas.png”.

Step 8

Once the icon is uploaded, go back to the AndroidManifest.xml file and find “@mipmap/ic_launcher” and “@mipmap/ic_launcher_round”. Replace both codes with “@drawable/bas” ( Replace the code with your icon name )
So replace “@mipmap/ic_launcher” with “@drawable/bas”

Step 9

Open MainActivity.java file and replace all code right under "import" with the code below. Don’t forget to replace your website’s name in the code below.

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

    String websiteURL = "https://www.computerbas.nl/"; // sets web url
    private WebView webview;

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

        if( ! CheckNetwork.isInternetAvailable(this)) //returns true if internet available
        {
            //if there is no internet do this
            setContentView(R.layout.activity_main);
            //Toast.makeText(this,"No Internet Connection, Chris",Toast.LENGTH_LONG).show();

            new AlertDialog.Builder(this) //alert the person knowing they are about to close
                    .setTitle("No internet connection available")
                    .setMessage("Please Check you're Mobile data or Wifi network.")
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    //.setNegativeButton("No", null)
                    .show();

        }
        else
        {

            //Webview stuff
            webview = findViewById(R.id.webView);
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setDomStorageEnabled(true);
            webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
            webview.loadUrl(websiteURL);
            webview.setWebViewClient(new WebViewClientDemo());

        }

    }

    private class WebViewClientDemo extends WebViewClient {
        @Override
        //Keep webview in app when clicking links
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;

        }

    }

    //set back button functionality
@Override
public void onBackPressed() { //if user presses the back button do this
    if (webview.isFocused() && webview.canGoBack()) { //check if in webview and the user can go back
        webview.goBack(); //go back in webview
    } else { //do this if the webview cannot go back any further
        new AlertDialog.Builder(this) //alert the person knowing they are about to close
                .setTitle("EXIT")
                .setMessage("Are you sure. You want to close this app?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .setNegativeButton("No", null)
                .show();
    }

}

}

class CheckNetwork {

    private static final String TAG = CheckNetwork.class.getSimpleName();

    public static boolean isInternetAvailable(Context context)

    {
        NetworkInfo info = (NetworkInfo) ((ConnectivityManager)

                context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

        if (info == null)

        {
            Log.d(TAG,"no internet connection");

            return false;

        }
        else
        {

            if(info.isConnected())

            {

                Log.d(TAG," internet connection available...");

                return true;

            }

            else

            {

                Log.d(TAG," internet connection");

                return true;
            }
        }
    }
}

Step 10

To enable screen rotation, simply add the below code in Android.Manifest.xml file in <application then inside <activity

android:screenOrientation="landscape"

Step 11

Run your application on the Virtual Mobile device. Press play button to check if the app is working.

Step 12

Build the Android apk file. All good?
Build the signed release bundle APK. This apk can be uploaded to the Google Play Store.

Thanks to:

TechsBucket

Yes you can!

Offline

Forum footer

Powered by FluxBB 1.5.11