How can I get the exact screen size using code?

I used the code below to find out the screen size, but I am not getting the correct values.

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); int height=display.getHeight(); 

It returns 320x480 for the galaxy tab. This is not true.

How to find the correct galaxy tab screen size of 600 x 1024.

+4
source share
4 answers

It seems that Android considers your application to be an β€œoutdated” application (ie for older versions of Android). From Multi-Screen Support :

For example, suppose this device uses a medium-density WVGA screen classified as a β€œlarge” screen, but the application claims to not support large screens; in this case, the system again "lies" on the application when it asks for screen sizes, and reports 320x480.

+1
source

I always use this code and have never encountered an error:

 Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); 
+3
source
 public static int getScreenWidth(Context context){ WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); return display.getWidth(); } public static int getScreenHeight(Context context){ WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); return display.getHeight(); } 
+1
source

in your program, the unit of width is px, your 600 may have a dp value, you can use the following code to check:

 final float scale = context.getResources().getDisplayMetrics().density; 

estimate the scale of attraction, if scale = 320/600 = 0.5333333, that is, 600 is the value of dp.

0
source

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


All Articles