Android WiFi On Off Inside App Enable Disable WiFi

Android WiFi On Off directly inside application Enable Disable WiFi

Make your great application with this pro feature suppose your app required internet connection you can tell user to connect android WiFI on off directly from your application. Here below is the source code for android WiFi On Off  source code. For beginner video tutorial is here android enable disable wifi. Read and know more from WiFi Android official link. ATTRACTION ANDROID is the source of latest android code application less deprecated code even supported with android newer API and android new OS Lollipop Marshmallow Nougat Oreo.

package Write your package name here;

import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button enableWiFi,disableWifi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        enableWiFi = (Button)findViewById(R.id.enable_wifi);
        disableWifi = (Button)findViewById(R.id.disable_wifi);

        disableWifi.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                wifi.setWifiEnabled(true);
            }
        });

        disableWifi.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                wifi.setWifiEnabled(false);
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#3bffd8"
    tools:context=".MainActivity">

    <TextView
        android:text="WiFi ON OFF EXAMPLE"
        android:id="@+id/out"
        android:textStyle ="bold"
        android:textSize ="30sp"
        android:layout_width ="wrap_content"
        android:layout_height ="wrap_content"
        android:layout_alignParentBottom ="true"
        android:layout_centerHorizontal ="true"
        android:layout_marginBottom ="18dp">
    </TextView>
    <Button
        android:id="@+id/enable_wifi"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="05dp"
        android:textColor="#ffffff"
        android:background="#128dff"
        android:textStyle="bold"
        android:textSize="25sp"
        android:text="ENABLE WiFi"
        android:layout_above="@+id/disable_wifi"
        />
    <Button
        android:id="@+id/disable_wifi"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:text="DISABLE WiFi"
        android:textColor="#ff0000"
        android:background="#ffffff"
        android:textStyle="bold"
        android:textSize="25sp"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="attraction.blessme.begin">

    <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="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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>

Search our page for latest android code. How to connect Bluetooth device from your own application. Visit our blog page for more.

Add a Comment

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