

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to enable wifi in android?
<p>This example demonstrate about How to enable wifi in android.</p><p><strong>Step 1</strong> − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.</p><p><strong>Step 2</strong> − Add the following code to res/layout/activity_main.xml.</p><pre class="prettyprint notranslate"><?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:gravity="center" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/text" android:textSize="30sp" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout></pre><p>In the above code, we have taken a text view to enable wifi.</p><p><strong>Step 3</strong> − Add the following code to src/MainActivity.java</p><pre class="prettyprint notranslate">package com.example.myapplication; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; @RequiresApi(api = Build.VERSION_CODES.N) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text); textView.setText("enable wifi"); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); wifiManager.setWifiEnabled(true); } }); } }</pre><p><strong>Step 4</strong> − Add the following code to AndroidManifest.xml</p><pre class="prettyprint notranslate"><?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.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/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <action android:name = "android.net.conn.CONNECTIVITY_CHANGE" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest></pre><p>Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run <img src="https://www.tutorialspoint.com/assets/questions/media/19830/play.jpg" class="fr-fic fr-dii" width="14" height="14"> icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –</p><p><img src="https://www.tutorialspoint.com/assets/questions/media/20137/enable_wifi.jpg" class="fr-fic fr-dib" style="width:278px; height:556px" width="278" height="556"></p><p>Click <a href="/android/projects/how_to_enable_wifi_in_android/MyApplication.zip" rel="nofollow" target="_blank">here</a> to download the project code</p>
- Related Questions & Answers
- How to disable wifi in android?
- How to enable Bluetooth in android?
- How to Use WiFi Direct on Android?
- How to check current wifi always discoverable in android?
- How to send data through wifi in android programmatically?
- How to enable webview java script in android?
- How to enable webview safe browsing in android?
- How to enable webview zoom controls in android?
- How to enable back button in android webview?
- How to enable webview mixed content in android?
- Android scan wifi networks programmatically
- How to programmatically turn on Wifi on Android device?
- How to I connect to wifi network on Android programmatically?
- How to enable app cache for webview in android?
- How to enable Web view session storage in android?
Advertisements