How to Programmatically Enable/Disable Wi-Fi in Android?
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 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 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" } } } } |