How to enable or disable Wifi in android programmatically

In this post, we will see how to enable or disable Wifi in android programmatically.
It is very simple to enable or disable wifi from android code.
For enabling WiFi:

For disabling Wifi:

So you can enable or disable WiFi using WifiManger class.
You need to add extra permissions in AndroidManifest.xml to enable or disable Wifi.

Source code:

Step 1 :

Create an android application project named “EnableDisableWiFiApp”.

Step 2:

Add following permissions in AndoridManifest.xml.

Go to app -> src -> main -> AndroidManifest.xml and add above permissions. AndroidManifest.xml will look like below xml.

Step 3 :

Change res ->layout -> activity_main.xml as below:

Step 4:

Change src/main/packageName/ as below:

Step 5:

Run the app
When you run the app, you will get below screen:

When you click on toggle button, you will get below screen:

Add Comment