Change WiFi State
Change WiFi State:
The meaning of changing the WiFi state in the case of Android, is to switch on or off the option of the particular device’s Wi-Fi connection.
Android devices have integrated features for Wi-Fi connectivity in which one can wirelessly connect to Wi-Fi networks in order to access internet or perform other Network operations.
Example:
<!-- AndroidManifest.xml -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="16dp"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:id="@+id/wifi_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change WiFi State"
android:textStyle="bold"
android:textSize="28sp"
android:textColor="#000000"
android:gravity="center"
android:layout_marginBottom="40dp"/>
<Button
android:id="@+id/wifi_on"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/wifi_state"
android:text="WiFi ON"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:background="@color/colorPrimaryDark"
android:layout_marginBottom="10dp"/>
<Button
android:id="@+id/wifi_off"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/wifi_on"
android:text="WiFi OFF"
android:textAllCaps="false"
android:textColor="@color/colorWhite"
android:background="@color/colorPrimaryDark"
android:layout_marginBottom="10dp"/>
</RelativeLayout>