Enable Disable WiFi programmatically in android on button click

How to dynamically Turn On and Turn Off WiFi from android application using WifiManager.

In this tutorial we are Turning On and Off the WiFi of android mobile phone device using switch button on-off event. We are handling WiFi connection using WifiManager whose work with android.net.wifi.WifiManager class. So here is the complete step by step tutorial for Enable Disable WiFi programmatically in android on button click.

android-project-download-code-button

How to Enable Disable WiFi programmatically in android on button click.

Add all these three permissions inside your AndroidManifest.xml file.

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

Code for MainActivity.java file.

 package com.enabledisablewifiprogrammatically_android_examples.com;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

public class MainActivity extends Activity {

 Switch WiFiButton;
 TextView textview;
 WifiManager wifimanager;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 WiFiButton = (Switch)findViewById(R.id.switch1);
 
 WiFiButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
 
 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 // TODO Auto-generated method stub
 
 if(isChecked)
 {
 textview.setText("WiFi ON");
 
 EnableWiFi() ;
 
 }
 else {
 textview.setText("WiFi OFF");
 
 DisableWiFi();
 
 }
 }
 });
 }
 
 public void EnableWiFi(){
 
 wifimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifimanager.setWifiEnabled(true);
 
 }
 
 public void DisableWiFi(){
 
 wifimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifimanager.setWifiEnabled(false);
 
 }
 
}

Code for activity_main.xml file.

<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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.enabledisablewifiprogrammatically_android_examples.com.MainActivity" >

 <Switch
 android:id="@+id/switch1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/switch1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="42dp"
 android:text="WiFi OFF"
 android:textAppearance="?android:attr/textAppearanceLarge" />
 
</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.enabledisablewifiprogrammatically_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="14"
 android:targetSdkVersion="21" />
 
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Screenshots:

wifi-off Enable Disable WiFi programmatically in android on button click

Click here to download Enable Disable WiFi programmatically in android on button click project with source code.

Leave a Reply

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