How to see if Wi-Fi is connected on Android?


Introduction

Many times in android application api calls are being made. For making this api call it is necessary that the device is connected to the internet. Most of the applications before making any api call make sure that the device is connected to the internet. Along with that when an application is downloading some big files in that case the application generally checks whether the device is connected to wifi or not. In this article we will take a look at How to see if the android device is connected to Wi-fi or not from our android application.

Implementation

We will be creating a simple project in which we will be displaying a simple text view for displaying the heading of our application. After that we will be creating a button. We will be using this button to check the current Wifi status within our application whether wifi is connected or not.

Step 1 : Creating a new project in Android Studio.

Navigate to Android studio as shown in below screen. In the below screen click on New Project to create a new Android Studio Project.

After clicking on New Project you will get to see the below screen.

Inside this screen we have to simply select Empty Activity and click on Next. After clicking on next you will get to see the screen below.

Inside this screen we have to simply specify the project name. Then the package name will be generated automatically.

Note − Make sure to select the Language as Java.

After specifying all the details click on Finish to create a new Android studio project.

Once our project has been created we will get to see 2 files which are open i.e activity_main.xml and MainActivity.java file.

Step 3 : Working with activity_main.xml

Navigate to activity_main.xml. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>activity_main.xml to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <!-- on below line creating a text view for displaying a heading-->
   <TextView
      android:id="@+id/idTVHeading"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:layout_margin="10dp"
      android:padding="4dp"
      android:text="Check Wi-Fi Connection Status in Android"
      android:textAlignment="center"
      android:textColor="@color/black"
      android:textSize="20sp"
      android:textStyle="bold" />

   <!-- on below line creating a button to check wifi status-->
   <Button
      android:id="@+id/idBtnCheckWifiStatus"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_below="@id/idTVHeading"
      android:layout_centerHorizontal="true"
      android:layout_margin="20dp"
      android:text="Check Wi-Fi Status"
      android:textAllCaps="false" />
</RelativeLayout>

Explanation − In the above code we are creating a Relative layout as a root layout and inside that we are creating a simple text view in which we are displaying the heading of our application. After that we are creating a button which we will be using to check the current wifi status of our device.

Step 4 : Working with MainActivity.java file.

Navigate to MainActivity.java. If this file is not visible. To open this file. In the left pane navigate to app>res>layout>MainActivity.java to open this file. After opening this file. Add the below code to it. Comments are added in the code to get to know in detail.

package com.example.androidjavaapp;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

   // on below line we are creating variable for button.
   private Button checkWifiBtn;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // on below line initializing variable with id.
      checkWifiBtn = findViewById(R.id.idBtnCheckWifiStatus);
      // on below line adding click listener for our button.
      checkWifiBtn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // on below line we are creating a variable for connectivity manager and initializing it.
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            // on below line we are getting network info to get wifi network info.
            NetworkInfo wifiConnection = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            // on below line displaying toast message when wi-fi is connected when wi-fi is disconnected
            if (wifiConnection.isConnected()) {
                  Toast.makeText(MainActivity.this, "Wi-Fi is connected..", Toast.LENGTH_SHORT).show();
               } else {
               Toast.makeText(MainActivity.this, "Wi-Fi is disconnected..", Toast.LENGTH_SHORT).show();
            }

         }
      });
   }
}

Explanation − In the above code we are firstly creating a variable for our button. After that we will get to see an onCreate method inside which we will be initializing the layout file which we have to display when the app is opened for the first time. Inside this onCreate method we are initializing our button with its id. After that we are adding an on click listener for our button. Inside the click listener for our button we are checking the wi-fi connection status using connectivity manager and network info. After that we are displaying the toast message when the wi-fi is connected or disconnected.

After adding the above code now we have to simply click on the green icon in the top bar to run our application on a mobile device.

Note − Make sure you are connected to your real device or emulator.

Output

Conclusion

In the above tutorial we have taken a look on How to check the Wi-Fi connection status for our device from our android application using connectivity manager and network info.

Updated on: 30-Mar-2023

868 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements