Search code examples
androidorientationtoast

How to avoid the toast when screen is on landscape mode in Android


I need to show the user a message using toast or snackbar. However, i do not want to show this message when the user tilts his/her phone into landscape mode. How can I achieve this message showing only in portrait mode and not in landscape mode?


Solution

  • This method will return true if the phone is in portrait and false if the phone is in Landscape it uses configurations:

    private boolean isPortrait(){
       return (getResources().getConfiguration().orientation==ORIENTATION_PORTRAIT);
    }
    

    Import configuration classes in Android Studio its easy. So from here you can use a simple if statement using the method given above:

    if(isPortrait()){
        //Show you Toast and snackbar here
    }
    

    For more information on this check the official documentation here.