Android orientation and screen size

I want to know if the device’s screen width is greater than the height like HTC chacha, that its screen width is 480 and the height is 320

I used this code to determine it

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); if(display.getWidth() > display.getHeight()) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

actual device screen size is 480w x 320h when i use this code i got 320w x 480h

+4
source share
3 answers

This code will return the width (w) and height (h) of the screen.

 DisplayMetrics dMetrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dMetrics); float density = dMetrics.density; int w = Math.round(dMetrics.widthPixels / density); int h = Math.round(dMetrics.heightPixels / density); 

activity is an instance of Activity for which you want to get the screen size.

You must remember that: when your device is in landscape orientation, w> h. When he is in portrait orientation w <h.

So, in width and height you can find that your device is in what orientation.

+4
source
 DisplayMetrics dimension= new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dimension); int w = dimension.widthPixels; int h = dimension.heightPixels; if(h>w){ //"portrait" } else{ //"landscape" } 
+3
source

Orientation determines which side is width or height. If you got 480x320, because when you call display.getWidth() , you are already in landscape mode. I do not know this device, but most likely the default orientation is the landscape when you hold it in a portrait ...

If you really need to know when you are in the presence of such a device, you can check the current orientation:

  getResources().getConfiguration().orientation 

and compare it with the result of the OrientationEventListener , which uses sensors to calculate the actual physical orientation in degrees.

+2
source

Source: https://habr.com/ru/post/1482398/


All Articles