How to detect auto if the device is horizontal in java

Asked

Viewed 22 times

0

I want to do a script on Java to detect if the device is horizontal or vertical, with if Else, want to be horizontal automatically hide the navigation bar of the device in setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

and if vertically display the navigation bar, someone wise to instruct me?

  • It’s an old question here on the site, but I had trouble locating it and now I don’t have time to do it. I tried looking for Android horizontal rotation and things like that. Maybe the tags have something. If anyone finds please mark as duplicate.

  • But on second thought I think this is solved with separate Xmls for the vertical and horizontal layouts, it would be something to be researched.

1 answer

0


You can detect the orientation of the device by overriding method onConfigurationChanged , as below:

 @Override
   public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         //horizontal
      } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
         //vertical
      }
   }

Example in the documentation

Browser other questions tagged

You are not signed in. Login or sign up in order to post.