Search code examples
androidwifitogglebuttonwifimanager

What does the ! mean before some phrases in android java?


I am stuck in this android app I am creating that has a toggle button. OnCreate I want it to get the wifi state, I have seen some people use ! In their code ex.(!isWifiEnabled) and I want to know what that ! means to see if I can implement that in my code instead of using the code below. This is all going in the onClick action of the ToggleButton.

if(wifiManager.isWifiEnabled ==true){
    wifiManager.setWifiEnabled(false);
}

Solution

  • The "!" means negation if a function returns true you are inverting the out put here that is

    !true isnothing but false and !false is true
    

    Your code is

    if(wifiManager.isWifiEnabled ==true){
    wifiManager.setWifiEnabled(false);
    

    }

    you can replace the if condition with

    if(wifiManager.isWifiEnabled)

    as long as it returns a boolean true value and if you want to check if wifi is not turned on then you can use

    if(!wifiManager.isWifiEnabled)

    hope this answer helped you