How to Programmatically Enable/Disable Wi-Fi in Android?
Last Updated :
15 Oct, 2020
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.

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
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wifi">
<!--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" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--Changes the state of Wi-Fi on button click-->
<Button
android:id="@+id/wifiSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click" />
<!--Displays the state of Wi-Fi on button click-->
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/wifiSwitch"
android:layout_centerHorizontal="true"
android:hint="Wifi Status"
android:textSize="30sp" />
</RelativeLayout>
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.
Kotlin
import android.net.wifi.WifiManager
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 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>(R.id.wifiSwitch)
val textView = findViewById<TextView>(R.id.tv)
// 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
Similar Reads
How to Programmatically Enable/Disable Bluetooth in Android? In Android Phone, it is very much easy to enable/disable Bluetooth by using the Bluetooth 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 pr
3 min read
How to Find Out Carrier's Name in Android Programmatically? In this article we will see how to retrieve the carrier name on Android device. This information can be useful for applications that need to provide specific functionality based on the user's cellular network provider. A sample video is given below to get an idea about what we are going to do in thi
3 min read
How to Enable/Disable Button in Android? The Enable/Disable Feature is used in many Android apps so the basic use of that feature is to prevent the user from clicking on a button until they will add all the required fields that have been asked. We are considering an example of email and password if the user has entered the email and passwo
3 min read
How to Detect Tablet or Phone in Android Programmatically? A Mobile is a portable electronic device that allows you to make calls, send messages, and access the internet, among other functions. A tablet is a mobile computing device with a touchscreen display and typically a larger screen size than a smartphone. Both devices are designed to be portable and a
3 min read
How to Get the MAC of an Android Device Programmatically? MAC stands for Media Access Control. The MAC address is also known as the Equipment Id Number. This MAC Address is provided by the Network Interface Card. In this article, we will see step by step from creating a new empty project to How to make an android app to display MAC Address using Java. Note
2 min read
How to Check GPS is On or Off in Android Programmatically? GPS (Global Positioning System) is a satellite-based navigation system that accommodates radio signals between satellite and device to process the device's location in the form of coordinates. GPS gives latitude and longitude values of the device. Recent mobile phones are equipped with GPS modules t
2 min read
How to Get the Device's IMEI/ESN Programmatically in Android using Jetpack Compose? Android applications many times require the unique identity of the user while developing applications. This unique identity is used to identify the user of the application. Many android apps use the IMEI number as the unique identity of each user. In this article, we will take a look at How to get D
3 min read
How to Check Airplane Mode State in Android Programmatically? Airplane Mode is often seen in action during flights, avoiding calls, or rebooting the network on mobiles, tablets, and laptops. Airplane mode is a standalone mode where the device turns down the radio communications. These may include Wifi, GPS, Telephone Network, Hotspot depending upon the year of
3 min read
How to Check if AUX is Plugged or Not in Android Programmatically? AUX (Auxiliary) port is a common port observed in most of the daily use electronic devices. It is a standard communication port for transferring audio signals from one device to another. We mostly observe them on mobile phones, computers, television sets, speakers, headphones, and headsets. While pl
2 min read
How to Obtain the Connection Information Programmatically in Android? Sometimes it becomes challenging to find the network-related details, especially the device's IP address, which could be needed to grant unique preferences through the modem software. Because of the variance in the information shown to the user across multiple Android devices (Samsung, Mi, Lava), we
4 min read