Kevik
Kevik

Reputation: 9361

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();

Upvotes: 8

Views: 11490

Answers (8)

M Ashhal
M Ashhal

Reputation: 467

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"

    }

Upvotes: 1

Mahesh Ambekar
Mahesh Ambekar

Reputation: 35

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;

Upvotes: 1

Kanifnath Modak
Kanifnath Modak

Reputation: 172

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. 

Upvotes: 2

Manu Zi
Manu Zi

Reputation: 2370

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

display.getRealSize();

Upvotes: 3

steevoo
steevoo

Reputation: 631

    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();;
    }

Upvotes: 0

Sanket
Sanket

Reputation: 3134

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;

Upvotes: 8

Bhanu Sharma
Bhanu Sharma

Reputation: 5145

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");

Upvotes: 0

Yashwant
Yashwant

Reputation: 33

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);

Upvotes: -1

Related Questions