Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get total screen size on an android device programmatically

if i use the code shown here to get the total screen size it is always short on height to the total screen size as shown in the documented specs for the device. for example I tried to get the screen size by using the code to get the size for a tablet listed as 1280 X 800 and the result from the code is: 1216 X 800. so where is the missing 64 pixels going to?

i can guess that it could be the bar at the bottom of the screen that holds the back and home buttons. but that does not sound right as the content views is supposed to be the root of all views. what is going on here?

the only possible explanation could be this part of the UI

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();
like image 894
Kevik Avatar asked Nov 28 '13 10:11

Kevik


1 Answers

If you're calling this outside of an Activity, you'll need to pass the context in (or get it through some other call). Then use that to get your display metrics:

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
like image 168
Sanket Avatar answered Sep 25 '22 23:09

Sanket