In this part of "Phones gallery" series we will see how to check the internet connection when the app start and show a dialog (no internet) if there is no internet.
AndroidManifest
Open android studio and choose your project then navigate to project files. Inside AndroidManifest.xml add this line under <manifest :
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Network check class
To check the network connection we need this class :
package m.phonesgallery.phonesgallery.helpers;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkCheck {
Context context;
public NetworkCheck(Context context) {
this.context = context;
}
public boolean isConnected(){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null && wifi.isConnected())||(mobile != null&& mobile.isConnected())){
return true;
}else {
return false;
}
}
}
No internet dialog
I create a custom no internet dialog :
- no_internet_dialog.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/splash_bg">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginHorizontal="10dp"
android:text="No internet!"
android:textColor="@color/white"
android:fontFamily="@font/ubuntu"
android:textStyle="bold"
android:textSize="25sp"
android:gravity="center"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="10dp"
android:src="@drawable/no_internet_vector"
android:layout_marginHorizontal="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No internet connection, please check your internet and try again!"
android:textColor="@color/white"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginHorizontal="20dp"
android:textSize="16sp"
android:fontFamily="@font/ubuntu"/>
<Button
android:id="@+id/RetryBtn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:backgroundTint="@color/orange1"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="10dp"
android:text="Retry"
android:textAllCaps="false"
android:textSize="16sp"
android:fontFamily="@font/ubuntu"
android:textStyle="bold"
android:layout_marginBottom="20dp"/>
</LinearLayout>
- NoInternetDialog.java :
package m.phonesgallery.phonesgallery.helpers;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import m.phonesgallery.phonesgallery.R;
public class NoInternetDialog {
Activity activity;
Dialog dialog;
public NoInternetDialog(Activity activity) {
this.activity = activity;
}
public void start(){
dialog = new Dialog(activity);
dialog.setContentView(R.layout.no_internet_dialog);
dialog.setCancelable(false);
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
Button retry = dialog.findViewById(R.id.RetryBtn);
retry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(activity,activity.getClass());
activity.startActivity(intent);
activity.finish();
dialog.dismiss();
}
});
dialog.show();
}
}
Note : we will need this class on the login screen ,signup screen and forgot password screen (for now)
Modifications to splash.java file
Finally we did some modifications to our splash.java and it will execute like this :
- Check if the app opened for the first time (if the first time, it will show the onBoarding screen without internet check).
- If it's not the first time it will check the internet at least 20 times.
- Inside those 20 times if it find internet connection it will go to the Login screen.
- If the internet checked 20 times and there is no internet, then it will show the no internet dialog.
- When the user click retry button (dialog button) it will restart the activity (the process will start again from 1).
Now lets check our splash.java file(comments everywhere so i hope it's usefull) :
package m.phonesgallery.phonesgallery;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import m.phonesgallery.phonesgallery.helpers.NetworkCheck;
import m.phonesgallery.phonesgallery.helpers.NoInternetDialog;
import m.phonesgallery.phonesgallery.helpers.saveState;
public class splash extends AppCompatActivity {
//XML
private TextView title,subtitle;
private ImageView logo;
//Animations
private Animation logoAnim,textsAnim;
//Helpers
NetworkCheck networkCheck;
NoInternetDialog noInternetDialog;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//XML
title = findViewById(R.id.splashTitle);
subtitle = findViewById(R.id.splashSubtitle);
logo = findViewById(R.id.splashLogo);
//Animations
logoAnim = AnimationUtils.loadAnimation(this,R.anim.logo_anim);
textsAnim = AnimationUtils.loadAnimation(this,R.anim.texts_anim);
//Helpers
networkCheck = new NetworkCheck(this);
noInternetDialog = new NoInternetDialog(this);
//to the next screen
toTheNextScreen();
}
private void toTheNextScreen(){
title.setAnimation(textsAnim);
subtitle.setAnimation(textsAnim);
logo.setAnimation(logoAnim);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
onBoardingCheckPoint();
}
},3000);
}
private void onBoardingCheckPoint() {
Intent i;
//we need the save state function here
saveState saveState = new saveState(splash.this,"onBoarding");
if (saveState.getState() == 0){
//the first time we install the app so no need to check internet just go to the onboarding screen
i = new Intent(splash.this,onBoarding.class);
startActivity(i);
finish();
}else{
// but here we enter the app not the first time so lets check internet
InternetCheckPoint();
}
}
private void InternetCheckPoint(){
i++;
//okey lets check the internet connection at least 20 times
if (i == 20){
//now here we have checked the internet 20 time but still not connected
//lets show the no internet dialog here
noInternetDialog.start();
}else if (networkCheck.isConnected()){
//here the device is connected to the internet
Intent i = new Intent(splash.this,Login.class);
startActivity(i);
finish();
}else if (!networkCheck.isConnected()){
//here the device is not connected to the internet so lets try again (20 times at least)
InternetCheckPoint();
}
}
}
Youtube
Check youtube video :
Thanks for watching.