Recently Added

6/recent/ticker-posts

How to Check Internet Connection in Android Studio - No Internet Connection Dialog Android Studio

 

In this blog, I'm going to create how we can check internet connection on Button click in the android studio
so let's start



1. we need to add a button in activity_main.xml
<?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">

<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


2. Now add this code to your MainActivity.class
package com.example.checkconnection;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.check);
button.setOnClickListener(v -> {
if (connected()) {
//do your task
Toast.makeText(this, "internet connected",
Toast.LENGTH_SHORT).show();
} else {
shoowCustomDialog();
Toast.makeText(this, "internet not connected",
Toast.LENGTH_SHORT).show();
}
});
}
private void shoowCustomDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("please connect to the internet to proceed")
.setCancelable(false)
.setPositiveButton("connect", ((dialog, which) -> {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
})).show();
}
private boolean connected() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
return networkInfo != null;
}
}


3. add this permission in your AndroidManifest


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



Watch Full Video Tutorials



Post a Comment

2 Comments

  1. CL 4K UHD Video Player - Video Player – App

    CL 4K UHD Player- High Quality Video Player app allows you to watch your favorite movies, shows and other videos in Ultra HD quality. You can watch high quality videos from your device or SD card and stream from web. This app also works as whatsapp status downloader.

    Install CL 4K UHD Player- High Quality Video Player on your android device and enjoy 4K ultra HD videos anytime, anywhere. Video Player

    ReplyDelete
  2. I truly like to read your post. Thank you so much for taking the time to share such nice information.

    Click here

    ReplyDelete