Full tutorial is given below. Dear Viewer we are working hard to maintain this website. We doesn't want any think from you. Its just a humble request If you thinks that this posts helps you please share this post with your friends.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.usmtip.myapplication"> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:layout_margin="16dp" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity"> <ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" /> </LinearLayout>
MainActivity.java
package com.usmtip.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.net.wifi.WifiManager; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.ToggleButton; public class MainActivity extends AppCompatActivity { ToggleButton toggleButton; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Getting toggle button and textView from activity_main toggleButton = (ToggleButton) findViewById(R.id.toggleButton); textView = (TextView) findViewById(R.id.textView); // Put listener on toggle button toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { if (checked) { textView.setText("WiFi is ON"); WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(true); } else { textView.setText("WiFi is OFF"); WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(false); } } }); // For initial setting if (toggleButton.isChecked()) { textView.setText("WiFi is ON"); WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(true); } else { textView.setText("WiFi is OFF"); WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(false); } } }
Post a Comment