1
    WIFI  = (Spinner) findViewById(R.id.WIFI);
 // Create an ArrayAdapter using the string array and a default spinner layout
 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
         R.array.WIFI, android.R.layout.simple_dropdown_item_1line);
 // Specify the layout to use when the list of choices appears
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 // Apply the adapter to the spinner
 WIFI.setAdapter(adapter);
 WIFI.setOnItemSelectedListener(new MyOnItemSelectedListener());

How to enable wifi by selecting it from spinner. I'll be thankful to you

Mickel
  • 45
  • 7

2 Answers2

2

Add this piece of code to your listener

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);

or for toggle behaviour

wifi.setWifiEnabled(!wifi.isWifiEnabled());

You will probably need to add some permissions to your manifest

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

See WifiManager for more options...

And as far as I know, there is no API for hotspot features. You would have to use reflection to call those methods...

user219882
  • 15,274
  • 23
  • 93
  • 138
  • if i add "wifi.setWifiEnabled(true)" in my code it always enabled the wifi. i just want when i select wifi it enable wifi. and whan i select wifi-Hotspot it enable hotspot. please guide – Mickel Nov 17 '12 at 11:07
  • @Mickel Well, that's what you asked for. You didn't say anything about hotspot... See the updated answer – user219882 Nov 17 '12 at 11:14
  • yes dear, you answer me correctly. but it enable wifi when application launch even without selection it enables wifi. and stops the mobile network. so i do that. Thanks alot for your help my friend. you guide me correctly but my requirement is different. – Mickel Nov 19 '12 at 12:15
  • for your help and time given me, i again accept your answer:) – Mickel Nov 19 '12 at 12:17
  • @Mickel Thank you :) My point was that there was a question and the correnct answer which should be accepted. This is how SO should work... If you want to help with another problem, just ask another question. It's better than asking two problems in one topic... – user219882 Nov 19 '12 at 12:24
  • My dear Tomas, i asked another question of this type. then SO stopped me to ask any question today:( so i asked it in one topic. next time I'll take care of it. – Mickel Nov 19 '12 at 12:29
  • " Sorry, we are no longer accepting questions from this account. See http://goo.gl/C1Kwu to learn more. " This is the error display when i am going to ask any question:( – Mickel Nov 19 '12 at 12:38
  • @Mickel that's because you duplicate your own questions... See http://stackoverflow.com/questions/13410502/how-to-add-wi-fi-option-in-gprs-spinner and http://stackoverflow.com/questions/13429462/how-to-add-option-of-wifi-in-gprs-spinner and this one... Why? And please don't post comments that are not related to the question. It might confuse other users... – user219882 Nov 19 '12 at 16:37
0

To do this you will need to get the text (or selection) of your spinner, then you will need to toggle the WiFi settings.

Getting the selected item on a spinner

  • Get spinner selected items text?

    String Text = mySpinner.getSelectedItem().toString();
    if (Text.equals("on"){
        //put below code here
    } else {
        //put more code here
    }
    

Toggling WiFi

Community
  • 1
  • 1
jcw
  • 5,132
  • 7
  • 45
  • 54
  • it enable wifi without selection too. i just want to select service by enabling it. without selection it doesn't work. how it is possible. Kindly help??? – Mickel Nov 19 '12 at 06:51