8

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();
Kevik
  • 9,181
  • 19
  • 92
  • 148
  • I think this link will explain everything : [Android dimension](http://stackoverflow.com/a/1016941/2927883) – Pull Nov 28 '13 at 10:06

8 Answers8

8

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;
Sanket
  • 3,094
  • 1
  • 16
  • 19
  • This worked perfectly. And from what I have seen so far, this will work from API 1. Some of the other options had me changing my minAPI to something higher when I did not want to change it any. – Matt Dec 05 '13 at 14:44
3

I'm not sure what api you use but if you use api starting from 17 you can use this:

display.getRealSize();
Robert
  • 5,278
  • 43
  • 65
  • 115
Manu Zi
  • 2,300
  • 6
  • 33
  • 67
2
try this code...

Display display = context.getWindowManager()
            .getDefaultDisplay();
        int width = display.getWidth();
    int height=display.getHeight();


it will give screen's width and height,And according to width and height write if-    conditions for each device's screen resolution. 
Kanifnath Modak
  • 172
  • 1
  • 8
1

You are right, It is showing you the resolution of your app screen and hence you getting the difference.Instead of getMetrics(), use getRealMetrics(). You can use the following code to get the actual screen size of 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
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");
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
0
    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();;
    }
steevoo
  • 621
  • 6
  • 18
0

After searching for hours through stackOverFlow, couldn't find a single answer that was up to date so thought I would share mine. Hope this helps. :)

Method 1 (Deprecated)

fun Resolution() : String {
        var width : Int? = 0
        var height : Int? = 0
        try {
            val displayMetrics = DisplayMetrics()
            val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            windowManager.defaultDisplay.getRealMetrics(displayMetrics)
            width = displayMetrics.widthPixels
            height = displayMetrics.heightPixels
        }catch (e : Exception){
            e.printStackTrace()
            Log.e("DisplayClass", e.message, e)
        }
        return "$width x $height pixels"
    }

Difference between getRealMetrics(displayMetrics) and getMetrics(displayMetrics) is that getMetrics() will return height/width without adding the size of status bars and navigation bars

windowManager.defaultDisplay.getRealMetrics(displayMetrics) // e.g: 1080 x 2340

windowManager.defaultDisplay.getMetrics(displayMetrics) // e.g: 1080 x 2260

Method 2 (Deprecated) (Minimum API : 30 (R))

This method returns the same result as getMetrics()

@RequiresApi(Build.VERSION_CODES.R)
    fun Resolution() : String {
        
        val width = context.display?.width
        val height = context.display?.height
        
        return "$width x $height"
        
    }

Method 3 (Working as of 24th August 2022)

fun Resolution() : String {
        var width: Int? = 0
        var height : Int? = 0

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){
            val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            val metrics = windowManager.currentWindowMetrics

            width = metrics.bounds.width()
            height = metrics.bounds.height()

        }else{
            val displayMetrics = DisplayMetrics()
            val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            windowManager.defaultDisplay.getRealMetrics(displayMetrics)
            width = displayMetrics.widthPixels
            height = displayMetrics.heightPixels
        }

        return "$width x $height"

    }
M Ashhal
  • 389
  • 2
  • 12
-1

Get all Display set your width and height as you required.

    Display display;
    display=getWindowManager().getDefaultDisplay();
    text1.setWidth(display.getWidth()-380);
    text1.setHeight(display.getHeight()-720);
Yashwant
  • 33
  • 8