Open In App
Related Articles

How to Programmatically Enable/Disable Wi-Fi in Android?

Like Article
Save Article
Report issue

In Android Phone, it is very much easy to enable/disable WiFi by using the WiFi icon, but have you wondered how to do this task programmatically in Android. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Sample WiFi

Steps for Programmatically Enable/Disable Wi-Fi 

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the AndroidManifest.xml file

Go to the AndroidManifest.xml file and add two user-permissions: ACCESS_WIFI_STATE and CHANGE_WIFI_STATE

<uses-permission android:name=”android.permission.ACCESS_WIFI_STATE”/>

<uses-permission android:name=”android.permission.CHANGE_WIFI_STATE”/>

Below is the code for the AndroidManifest.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--Put the permissions between the manifest and application opening tags-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


Step 3: Working with the activity_main.xml file

When the setup is ready, go to the activity_main.xml file, which represents the UI of the project. Create a Button that changes the state of the Wi-Fi on click and a TextView that shows the status of the Wi-Fi state. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.


<?xml version="1.0" encoding="utf-8"?>
    <!--Changes the state of Wi-Fi on button click-->
        android:text="Click" />
    <!--Displays the state of Wi-Fi on button click-->
        android:hint="Wifi Status"
        android:textSize="30sp" />


Step 4: Working with the MainActivity.kt file

In the MainActivity.kt file, declare the Button, TextView, and a Wi-Fi manager (refer to the codes). While setting the on click listeners to the button, we would use the Wi-Fi manager to enable or disable the Wi-Fi. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.annotation.RequiresApi
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring Button and TextView
        // 1. Changes the state of Wi-Fi on button click
        // 2. Shows the state of the Wi-Fi
        val btn = findViewById<Button>(
        val textView = findViewById<TextView>(
        // Declaring Wi-Fi manager
        val wifi = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
        // On button Click
        btn.setOnClickListener {
            // wifi.isWifiEnabled is a boolean, is Wi-Fi is ON, 
            // it switches down and vice-versa
            wifi.isWifiEnabled = !wifi.isWifiEnabled
            // For displaying Wi-fi status in TextView
            if (!wifi.isWifiEnabled) {
                textView.text = "Wifi is ON"
            } else {
                textView.text = "Wifi is OFF"


Output: Run on Emulator

Last Updated : 15 Oct, 2020
Like Article
Save Article
Share your thoughts in the comments
Similar Reads