In this tutorial, I will tell you how to Enable or Disable Wifi In Android programmatically with Kotlin. It’s a quite simple feature and we will perform it in Android Studio using Kotlin Language. So let’s start making our application.
In our XML we will take two buttons and we are going to perform Wifi Enable and Disable Function on these Buttons.
Enable or Disable Wifi In Android
1- Start a new Android Studio Project and name it “Wifi” or you can name it whatever you want. Select API level 19 and select the default language as Kotlin.
2- First of all go to your AndroidManifest.xml file and add these two permissions.
1 2 |
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> |
3- Go to activity_main.xml and change the default Constraint Layout to Relative Layout. Set the gravity of your Relative layout as “center”.
1 |
android:gravity="center" |
4- In the activity_main.xml file take two buttons. Set width = 200dp and height wrap_content.
5- Assign ids and some attributes to both the Views. Finally, it will look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<Button android:id="@+id/wifiOn" android:layout_width="200dp" android:layout_height="wrap_content" android:text="Wifi On" android:textSize="25sp" android:background="#25D366" /> <Button android:id="@+id/wifiOff" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_below="@+id/wifiOn" android:layout_marginTop="10dp" android:text="Wifi Off" android:textSize="25sp" android:background="#F44336"/> |
6- The final activity_main.xml file will look like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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" android:gravity="center" tools:context=".MainActivity"> <Button android:id="@+id/wifiOn" android:layout_width="200dp" android:layout_height="wrap_content" android:text="Wifi On" android:textSize="25sp" android:background="#25D366" /> <Button android:id="@+id/wifiOff" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_below="@+id/wifiOn" android:layout_marginTop="10dp" android:text="Wifi Off" android:textSize="25sp" android:background="#F44336"/> </RelativeLayout> |
MainActivity.kt
7- Now we will start our Kotlin code. Come to MainActivity.kt file and define a var wifiManager of type WifiManager.
1 |
private var wifiManager: WifiManager? = null |
8- In the onCreate method, we will define our wifiManager variable like this.
1 2 |
wifiManager = applicationContext. getSystemService(Context.WIFI_SERVICE) as WifiManager? |
9- We will make two functions. One to Enable the Wifi and the other one to disable the Wifi. Once Wifi connects or disconnects the app will display a Toast message Wifi Connected / Wifi Disconnected.
1 2 3 4 5 6 7 8 9 |
private fun enableWifi() { wifiManager!!.setWifiEnabled(true) } private fun disableWifi() { wifiManager!!.setWifiEnabled(false) } |
10- We call these functions from our Buttons with the help of an onClickListener.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
wifiOn.setOnClickListener(View.OnClickListener { enableWifi() if (!wifiManager!!.isWifiEnabled ) { Toast.makeText(this,"Wifi Connected", Toast.LENGTH_SHORT).show() } }) wifiOff.setOnClickListener(View.OnClickListener { disableWifi() if (wifiManager!!.isWifiEnabled ) { Toast.makeText(this,"Wifi Disconnected", Toast.LENGTH_SHORT).show() } }) |
Note: wifiOn and wifiOff are the ids of our buttons in XML.
11- Here We are done. The complete MainActivity.kt fill will be like this …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.example.wifi import android.content.Context import android.net.wifi.WifiManager import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Toast import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { private var wifiManager: WifiManager? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) wifiManager = applicationContext. getSystemService(Context.WIFI_SERVICE) as WifiManager? wifiOn.setOnClickListener(View.OnClickListener { enableWifi() if (!wifiManager!!.isWifiEnabled ) { Toast.makeText(this,"Wifi Connected", Toast.LENGTH_SHORT).show() } }) wifiOff.setOnClickListener(View.OnClickListener { disableWifi() if (wifiManager!!.isWifiEnabled ) { Toast.makeText(this,"Wifi Disconnected", Toast.LENGTH_SHORT).show() } }) } private fun enableWifi() { wifiManager!!.setWifiEnabled(true) } private fun disableWifi() { wifiManager!!.setWifiEnabled(false) } } |
OUTPUT


People Also Read
- ListView In Android Using Kotlin
- Kotlin Speech To Text Example
- Kotlin Text To Speech Working Example
- How To Check Internet Connection in Android