Enable or Disable Wifi In Android

Enable or Disable Wifi In Android Programmatically In Kotlin

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 as “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.

 

3- Go to activity_main.xml and change the default Constraint Layout to Relative Layout. Set the gravity of your Relative layout as “center”.

 

 

4- In 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.

 

6- The final activity_main.xml file will look like this.

 

MainActivity.kt 

 

7- Now we will start our Kotlin code. Come to MainActivity.kt file and define a var wifiManager of type WifiManager.

 

 

8- In onCreate method we will define our wifiManager variable like this.

 

 

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.

 

 

10- We call these functions from our Buttons with the help of an onClickListener.

 

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 …

 

 

 

OUTPUT 

 

Enable or Disable Wifi In Android
Enable or Disable Wifi In Android

 

 

Enable or Disable Wifi In Android
Enable or Disable Wifi In Android

Enable or Disable Wifi In Android

        People Also Read

 

 

 

5 thoughts on “Enable or Disable Wifi In Android Programmatically In Kotlin”

  1. Pingback: Model Number and IMEI In Android Kotlin - My Android

  2. Pingback: Android Indeterminate Progress Bar: How to create Progress Bar

  3. Pingback: How To Check Internet Connection in Android Programmatically

  4. Pingback: Stylish Progress Bar In Android With Kotlin | Android Example - My Android

  5. Pingback: RecyclerView With Data Binding in Android Kotlin: Example With Kotlin

Leave a Comment

Your email address will not be published. Required fields are marked *