0

I use this simple condition check to show a Toast but none of them is shown:

 public void ShowVersion(){

         //This works So I am sure the method is called:
         //Toast.makeText(this,"Method is called",Toast.LENGTH_LONG).show();

        if (Build.VERSION.SDK_INT >= 23) {
            Toast.makeText(this,"Version Supported",Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this,"Version not supported",Toast.LENGTH_LONG).show();
        }
    }

How it is possible that none of if and else work?

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
  • `How it is possible that none of if and else work?` Meaning that Toast is not showing up? – Blackbelt Aug 06 '18 at 13:16
  • Yes I mean that. The toast works when it is out of if/else so I think none of those conditions are true @Blackbelt – Ali Sheikhpour Aug 06 '18 at 13:17
  • it is not possible for both branches of an if/else to not be executed. If you step through it with a debugger you will see that one of them is executed. Probably there is another issue, show more code – Tim Aug 06 '18 at 13:29

2 Answers2

0

The main problem was an exception inside the IF block. The exceptions breaks IF / ELSE block. A try/catch block can help detect problem in similar cases.

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
-1

Try this:

public void ShowVersion(){

     //This works So I am sure the method is called:
     //Toast.makeText(this,"Method is called",Toast.LENGTH_LONG).show();

    if (android.os.Build.VERSION.SDK_INT>=11) {
        Toast.makeText(this,"Version Supported",Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this,"Version not supported",Toast.LENGTH_LONG).show();
    }
}

OR a more elegant way would be:

public void ShowVersion(){

     //This works So I am sure the method is called:
     //Toast.makeText(this,"Method is called",Toast.LENGTH_LONG).show();

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        Toast.makeText(this,"Version Supported",Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this,"Version not supported",Toast.LENGTH_LONG).show();
    }
}
Jacob Celestine
  • 1,758
  • 13
  • 23