Use the following code snippets to turn on and off wifi on Android devices.

Switch toggle = (Switch) findViewById(;
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

public void toggleWiFi(boolean status) {
	WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
	if (status == true && !wifiManager.isWifiEnabled()) {
	} else if (status == false && wifiManager.isWifiEnabled()) {

Please note, for changing wifi state, you need the following permissions in your manifest file.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
Tagged in : #Android #Java


Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.