How to get screen size in Android, including virtual buttons?

For normal use, you can use the DisplayMetrics class and get the “visualized” size, but I want to find out the actual size of the physical screen, which includes the height of the virtual buttons.

In my Galaxy Nexus, the size of the report, regardless of what I tried, is 1196x720, what can I use to get the physical version of 1280x720? The same goes for Nexus 7, Nexus 4, and Nexus 10 devices.

UPDATE This is the last code I'm using now, including the fix:

//Activity A = this; DisplayMetrics displayMetrics = new DisplayMetrics(); WindowManager wm = (WindowManager) A.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); Display disp = wm.getDefaultDisplay(); int API_LEVEL = android.os.Build.VERSION.SDK_INT; if (API_LEVEL >= 17) { disp.getRealMetrics(displayMetrics); } else { disp.getMetrics(displayMetrics); } int Width = displayMetrics.widthPixels; int Height = displayMetrics.heightPixels; 
+4
source share
2 answers

You should use Display.getRealSize (Point) From white papers here

The display area is described in two different ways.

The application display area indicates the display part that may contain the application window, with the exception of system decorations.

The application display area may be smaller than the actual display area because the system subtracts the space required for decor elements, such as a status bar. Use the following methods to query the application for the display area: getSize (dot), getRectSize (Rect) and getMetrics (DisplayMetrics).

The actual display area indicates a portion of the display containing content including system decorations. Despite this, the actual display area may be smaller than the physical size of the display if the window manager emulates a smaller display using (adb shell am display-size). Use the following methods to query. real display area: getRealSize (Point), getRealMetrics (DisplayMetrics).

+5
source

The answer in a similar question seems to have a solution to get the actual display size when the API <17 fooobar.com/questions/624 / ...

 WindowManager w = activity.getWindowManager(); Display d = w.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); d.getMetrics(metrics); // since SDK_INT = 1; widthPixels = metrics.widthPixels; heightPixels = metrics.heightPixels; // includes window decorations (statusbar bar/menu bar) if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) try { widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d); heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d); } catch (Exception ignored) { } // includes window decorations (statusbar bar/menu bar) if (Build.VERSION.SDK_INT >= 17) try { Point realSize = new Point(); Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize); widthPixels = realSize.x; heightPixels = realSize.y; } catch (Exception ignored) { } 
+4
source

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


All Articles