Introduction

In this article, I will show you how to Enable and Disable WI-FI in Android apps using Android Studio. The information that an application can access includes connected network's link speed, IP address, negotiation state, other network information. Applications can also scan, add, save, terminate, and initiate Wi-Fi connections.
Requirements
Steps should be followed
These are the steps to create a changed background and audio player Android app. I have included the source code below.
Step 1
Open Android Studio and start a new Android Studio Project.
Android
Step 2
You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
Android
Step 3
Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role in running the application.
Android
Step 4
Now, add the activity and click the "Next" button.
Android
Step 5
Add activity name and click "Finish".
Android
Step 6
Go to activity_main.xml. This XML file contains the designing code for the Android app in the activity_main.xml.
Android
The XML code is given below
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity">
  6. <ImageView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:id="@+id/imageView"
  10. android:src="@drawable/wifi"
  11. android:layout_alignParentTop="true"
  12. android:layout_alignParentStart="true"
  13. android:layout_marginStart="14dp" />
  14. <Button
  15. android:id="@+id/button1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_marginLeft="76dp"
  19. android:text="Enable Wifi"
  20. android:layout_alignParentStart="true"
  21. android:layout_marginStart="28dp"
  22. android:layout_alignBottom="@+id/button2" />
  23. <Button
  24. android:id="@+id/button2"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="Disable Wifi"
  28. android:layout_marginBottom="52dp"
  29. android:layout_marginEnd="14dp"
  30. android:layout_alignParentBottom="true"
  31. android:layout_alignParentEnd="true" />
  32. </RelativeLayout>
Step 7
Go to Main Activity.java. This Java program is the backend language for Android.
Android
The java code is given below
  1. package abu.wifi;
  2. import android.net.wifi.WifiManager;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.content.Context;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;
  9. public class MainActivity extends Activity {
  10. Button enableButton,disableButton;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. enableButton=(Button)findViewById(R.id.button1);
  16. disableButton=(Button)findViewById(R.id.button2);
  17. enableButton.setOnClickListener(new OnClickListener(){
  18. public void onClick(View v){
  19. WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  20. wifi.setWifiEnabled(true);
  21. }
  22. });
  23. disableButton.setOnClickListener(new OnClickListener(){
  24. public void onClick(View v){
  25. WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  26. wifi.setWifiEnabled(false);
  27. }
  28. });
  29. }
  30. }
Step 8
We need to Enable and Disable WI-FI. So, add WI-FI state permission in an AndroidManifest.xml.
Android
The AndroidManifest.xml Code is given below
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="abu.wifi">
  4. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  5. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. </application>
  20. </manifest>
Step 9
Now, go to the menu bar and click "Make a project" or press ctrl+f9 to debug it for errors.
Android
Step 10
Then, click the Run button or press shift+f10 to run the project. And choose the virtual machine and click OK.
Android

Conclusion

We have successfully created the "Enabled and Disabled WI-FI" Android application using Android Studio.
Android