How to Convert Website into Android appliaction using Android Studio.

Convert Website to Android application- Build a Mobile App for your own web URL using WebView in Android. You can roll your own web browser or simply display some online content within your application.

Create New Project

1. First create a new project in Android Studio (File => New Project)
2. Add EventBus dependencies in build.gradle(Module: app).

build.gradle(Module: app)


compile 'org.greenrobot:eventbus:3.0.0'
	

Add INTERNET permission in AndroidManifest.xml. After create Broadcast Receiver class than add receiver in AndroidManifest file.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.narendra.mydemoapp">
	
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
	
	<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
	
	<activity android:name=".MainActivity"/>
	<activity
            android:name=".Splash"
            android:theme="@style/Theme.AppCompat.NoActionBar">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
   </activity>
   
    <receiver
            android:name=".NetworkChangeReceiver"
            android:label="NetworkChangeReceiver">
            
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            </intent-filter>
        </receiver>
   
  </application>
</manifest>
	


Create a new Android resource (xml) file for Internet Connection Error. If Internet not available than display this screen automatically.

netlayout.xml

									   
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/neterror"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/netimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:src="@drawable/nointernet" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/netimg"
        android:layout_gravity="center"
        android:layout_marginEnd="121dp"
        android:text="Waiting for Net"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#f43260" />

    <TextView
        android:id="@+id/retry"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:background="#f53464"
        android:gravity="center"
        android:text="If Network is connect automatically redirect" />

    <ProgressBar
        android:id="@+id/netpro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/retry"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="60dp" />
		
    </RelativeLayout>

Now Open activity_main.xml and add layout file netlayout.

activity_main.xml

									   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.narendra.mydemoapp.MainActivity">

    <include layout="@layout/netlayout"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:id="@+id/mainlayout"
        android:layout_height="match_parent">

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </RelativeLayout>
</LinearLayout>

Now create a new Blank Activity. Change LAUNCHER activity in Manifest file.
Open splash xml file and add below code.

activity_splash.xml

									   
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/sp_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sp_img"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="Put your logo"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

Open Splash class and Edit with below code.

Splash.java

									   
package com.narendra.mydemoapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;

public class Splash extends AppCompatActivity {

    Context context;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        context = this;

        handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(context, MainActivity.class));
                finish();
            }
        }, 2500);
    }
}

Create a new java class file. In this class we check internet connection state

Network.java

									   
package com.narendra.mydemoapp;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Network {

    public static String activityname;
    private static boolean isConnected = false;

    public static boolean isConnectingToInternet(Context _context) {
        ConnectivityManager connectivity = (ConnectivityManager) _context.
		getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
        }
        return false;
    }
}

Create a new Java class. It is BroadcastReceiver class in this class we check internet available or not.

NetworkChangeReceiver.java

									   
package com.narendra.mydemoapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

public class NetworkChangeReceiver extends BroadcastReceiver {
    static EventBus bus = EventBus.getDefault();
    @Override
    public void onReceive(final Context context, final Intent intent) {

        String status = NetworkUtil.getConnectivityStatusString(context);
        Toast.makeText(context, status, Toast.LENGTH_SHORT).show();
        if(status != null) {
            if (status.equals("Not connected to Internet")){
            }
            else {
                Toast.makeText(context, "connected", Toast.LENGTH_SHORT).show();
                try {
                    bus.post(Network.activityname);
                    Log.w("-------","connect ");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

class NetworkUtil {

    public static int TYPE_WIFI = 1;
    public static int TYPE_MOBILE = 2;
    public static int TYPE_NOT_CONNECTED = 0;
    public static int getConnectivityStatus(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (null != activeNetwork) {
            if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
                return TYPE_WIFI;

            if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
                return TYPE_MOBILE;
        }
        return TYPE_NOT_CONNECTED;
    }

    public static String getConnectivityStatusString(Context context) {
        int conn = NetworkUtil.getConnectivityStatus(context);
        String status = null;
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = "Wifi enabled";
        } else if (conn == NetworkUtil.TYPE_MOBILE) {
            status = "Mobile data enabled";
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
        }
        return status;
    }
}

Now open MainActivity class and add below code .

MainActivity.java


package com.narendra.mydemoapp;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class MainActivity extends AppCompatActivity {

    Context context;
    WebView webView;
    WebSettings webSettings;
    RelativeLayout netLayout, mainlayout;
    EventBus bus = EventBus.getDefault();
    NetworkChangeReceiver myReceiver = new NetworkChangeReceiver();
    private ProgressDialog progDailog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        Network.activityname = "webview";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            registerReceiver(myReceiver, new IntentFilter(ConnectivityManager.
			CONNECTIVITY_ACTION));
        }
        if (!bus.isRegistered(this)) {
            Log.w("-------", " register bus 1111 ");
            bus.register(this);
        }

        mainlayout = (RelativeLayout) findViewById(R.id.mainlayout);
        netLayout = (RelativeLayout) findViewById(R.id.neterror);
        webView = (WebView) findViewById(R.id.webview);
        if (Network.isConnectingToInternet(context)) {
            mainlayout.setVisibility(View.VISIBLE);
            netLayout.setVisibility(View.GONE);
            Log.w("------", "on create if net ");
            webshow();
        } else {
            Log.w("------", "on create else no net ");
            mainlayout.setVisibility(View.GONE);
            netLayout.setVisibility(View.VISIBLE);
        }
    }

    private void webshow() {
        Log.w("------", "web show call ");
        progDailog = ProgressDialog.show(context, "Loading", "Please wait...", true);
        progDailog.setCancelable(false);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                progDailog.show();
                view.loadUrl(url);

                return true;
            }

            @Override
            public void onPageFinished(WebView view, final String url) {
                progDailog.dismiss();
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, 
			String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                netLayout.setVisibility(View.VISIBLE);
                mainlayout.setVisibility(View.GONE);
                Toast.makeText(context, "Something is wrong", Toast.LENGTH_SHORT).show();
            }
        });
        webView.loadUrl("https://www.globehost.com/");
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Subscribe
    public void onEvent(String name) {
        if (name.equalsIgnoreCase("webview") && netLayout.getVisibility() == View.VISIBLE) {
            netLayout.setVisibility(View.GONE);
            mainlayout.setVisibility(View.VISIBLE);
            Log.w("------", "on event ");
            webshow();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            unregisterReceiver(myReceiver);
        }
    }
}
	

Output :


  

Tags

  • Android

  • Android Studio

  • Android development

  • Java