How to get the total screen size on an Android device

if I use the code given here to get the total screen size, it is always short in height to the total screen size, as shown in the document specifications for the device. for example, I tried to get the screen size using the code to get the size for the tablet specified as 1280 X 800, and the result from the code: 1216 X 800. so where are 64 pixels missing?

I can guess that it could be a panel at the bottom of the screen that holds the back and home buttons. but that doesn’t sound like that, since content submissions must be the root of all submissions. what's going on here?

the only possible explanation could be this part of the user interface

is this the exception to total screen size?

code used to get screen size

// gets the content view windows width and height size DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int widthContentView = displaymetrics.widthPixels; int heightContentView = displaymetrics.heightPixels; Toast.makeText(MainActivity.this, "width of content view: " + widthContentView Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, "height of content view: " + heightContentView, Toast.LENGTH_SHORT).show(); 
+6
source share
8 answers

If you call this outside the scope of the action, you need to pass the context (or get it through some other call). Then use this to get display metrics:

 DisplayMetrics metrics = context.getResources().getDisplayMetrics(); int width = metrics.widthPixels; int height = metrics.heightPixels; 
+7
source

I'm not sure what you are using, but if you are using api starting at 17, you can use this:

 display.getRealSize(); 
+2
source
 try this code... Display display = context.getWindowManager() .getDefaultDisplay(); int width = display.getWidth(); int height=display.getHeight(); it will give screen width and height,And according to width and height write if- conditions for each device screen resolution. 
+2
source
 int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; switch(screenSize) { case Configuration.SCREENLAYOUT_SIZE_LARGE: Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show(); break; case Configuration.SCREENLAYOUT_SIZE_NORMAL: Toast.makeText(this, "Normal screen",Toast.LENGTH_LONG).show(); break; case Configuration.SCREENLAYOUT_SIZE_SMALL: Toast.makeText(this, "Small screen",Toast.LENGTH_LONG).show(); break; default: Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show(); } 
+1
source
 int density; private DisplayMetrics metrics; private int widthPixels; private float scaleFactor; private float widthDp; metrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); density= getResources().getDisplayMetrics().densityDpi; widthPixels = metrics.widthPixels; scaleFactor = metrics.density; widthDp = widthPixels / scaleFactor; System.out.println("widthDp== "+widthDp ); if(density==DisplayMetrics.DENSITY_HIGH) System.out.println("Density is high"); if(density==DisplayMetrics.DENSITY_XXHIGH) System.out.println("Density is xxhigh"); if(density==DisplayMetrics.DENSITY_XXXHIGH) System.out.println("Density is xxxhigh"); if(density==DisplayMetrics.DENSITY_TV) System.out.println("Density is Tv"); 
0
source
  int Measuredwidth = 0; int Measuredheight = 0; Point size = new Point(); WindowManager w = getWindowManager(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { w.getDefaultDisplay().getSize(size); Measuredwidth = size.x; Measuredheight = size.y; } else { Display d = w.getDefaultDisplay(); Measuredwidth = d.getWidth(); Measuredheight = d.getHeight();; } 
0
source

You are right. It shows you the screen resolution of your application and therefore you get the difference. Instead of getMetrics (), use getRealMetrics (). You can use the following code to get the actual screen size of the device.

  WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getRealMetrics(metrics); int width = metrics.widthPixels; int height = metrics.heightPixels; return width + "*" + height; 
0
source

Get All Display Set width and height as needed.

  Display display; display=getWindowManager().getDefaultDisplay(); text1.setWidth(display.getWidth()-380); text1.setHeight(display.getHeight()-720); 
-1
source

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


All Articles