Blogs chevron_right Codding chevron_right Codes for converting Website into Android Apps using Android Studio.

Codes for converting Website into Android Apps using Android Studio.

Convert Website into Android App using Android Studio. It is very easy to create. Here I am going to give you only the codes, if you are a beginner then this blog is not for you.

05,Jan
2020

0

Comments

102

Views




Here is the codes, just see the codes and add the required codes that is not there on your Empty Project Android Studio .

Just remember that you create an Empty project and start writing.

MainActivity.java

public class MainActivity extends AppCompatActivity { WebView webView; private String webUrl = "https://example.com"; ProgressDialog progressDialog; RelativeLayout relativeLayout; Button btnNoInternetConnection; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*HIDDING THE ACTION BAR START*/ try { this.getSupportActionBar().hide(); }catch (Exception e){ Toast.makeText(getApplicationContext(),""+e,Toast.LENGTH_SHORT).show(); } webView = (WebView) findViewById(R.id.myWebView); progressDialog = new ProgressDialog(this); progressDialog.setMessage("Loading Please Wait"); btnNoInternetConnection = (Button) findViewById(R.id.btnNoConnection); relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); checkConnection(); webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient()); webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { progressDialog.show(); if(newProgress==100){ progressDialog.dismiss(); } super.onProgressChanged(view, newProgress); } }); btnNoInternetConnection.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkConnection(); } }); } @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?") .setNegativeButton("No",null) .setNegativeButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finishAffinity(); } }).show(); } } public void checkConnection(){ ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if(wifi.isConnected()){ webView.loadUrl(webUrl); webView.setVisibility(View.VISIBLE); relativeLayout.setVisibility(View.GONE); } else if (mobileNetwork.isConnected()){ webView.loadUrl(webUrl); webView.setVisibility(View.VISIBLE); relativeLayout.setVisibility(View.GONE); }else { webView.setVisibility(View.GONE); relativeLayout.setVisibility(View.VISIBLE); } } }

reslayoutactivity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/myWebView" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relativeLayout"> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/no_internet" android:layout_centerHorizontal="true" android:id="@+id/noConnectionLogo" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="No Internet Connection" android:layout_below="@id/noConnectionLogo" android:textAlignment="center" android:textSize="26sp" android:id="@+id/txtNoConnection" /> <Button android:layout_width="140dp" android:layout_height="55dp" android:text="Retry" android:background="@color/colorAccent" android:textColor="#ffffff" android:textSize="22sp" android:layout_below="@id/txtNoConnection" android:layout_centerHorizontal="true" android:layout_marginTop="60dp" android:id="@+id/btnNoConnection" /> </RelativeLayout> </LinearLayout>

1. Add Images on

resdrawable

Here is the name of the files no_internet.png and shoppingmall.png

2. Add new Empty activity

Right click on the app and

NewActivityEmpty Activity

And name it as SplashActivity

Edit javacom.example.exampleSplashActivity

public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_splash); getSupportActionBar().hide(); Thread splashThread = new Thread(){ public void run(){ try { sleep(1000); startActivity(new Intent(getApplicationContext(),MainActivity.class)); finish(); } catch (InterruptedException e) { e.printStackTrace(); } super.run(); } }; splashThread.start(); } }

Edit reslayoutactivity_splash.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".SplashActivity"> <ImageView android:layout_width="match_parent" android:layout_height="300dp" android:src="@drawable/shoppingmall_logo" android:scaleType="centerCrop" android:padding="50dp" android:layout_marginTop="30dp" /> <ProgressBar android:layout_width="220dp" android:layout_height="10dp" android:layout_gravity="center_horizontal" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:indeterminate="true" android:progress="0" android:layout_marginTop="150dp" /> </LinearLayout>

manifestsAndroidMenifests.xml

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

Change the .MainActivity to .SplashActivity and .SplashActivity to .MainActivity

<activity android:name=".MainActivity"></activity> <activity android:name=".SplashActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

And the Last step, Change the Icon of the Launch App.


And That's it .. Hop it works for you.

And remember that don't copy and paste it, you must write it with your own hand on Android Studio.

Otherwise it may not work.


Mritunjoy Mushahary

Author of the Content

Founder, Creator and CEO of mritunjoy.com. Working



Please Login to leave a Comments

WelCome to the World of Digital Expression

Transform your Works into Digital and Makes your life more easier.

121

Total users

121

Active users

5

Active Earners

location_on BTR,Assam,INDIA
(Bodoland)
email [email protected]
call +91 8486866741
rss_feed

Copyright ©2019-20 mritunjoy.com,All Rights Reserved