This will not work in all cases as the user is enabled to change the Wifi state at every time and your application can not recognize this with your code. For example if togglewifi=true
because you saved the state somewhere earlier then you set your button to checked but that does not mean that the wifi is enabled. The exclusive or will not check the second condition as TRUE || FALSE = TRUE
and TRUE || TRUE = TRUE
. The outcome in this case only depends on the first parameter. Fix for your if then else construct:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
ToggleButton wifiToggle = (ToggleButton) findViewById(R.id.wifitoggle);
wifiToggle.setChecked(wifiManager.isWifiEnabled());