androidandroid-studiowidthsamsung-galaxy

How to get correct screen width (px) in Android Studio?


I have a physical device hooked up to my Android Studio - a Samsung Galaxy S20 Ultra 5G - SM-G988U. Everywhere I look online it lists the dimensions as 1440px x 3200px and 511 ppi.

However when I hook this phone up to android studio and I run my app I get the length and width measurements in Logcat (via the code below) and it returns 1080 x 2400.

int screenWidth = 0;
int screenHeight = 0;

if (Build.VERSION.SDK_INT >= 28 && Build.VERSION.SDK_INT < 30) {

            screenHeight = getResources().getDisplayMetrics().heightPixels;
            screenWidth = getResources().getDisplayMetrics().widthPixels;

} else {

            WindowMetrics deviceWindowMetrics = getApplicationContext().getSystemService(WindowManager.class).getMaximumWindowMetrics();
            screenWidth = deviceWindowMetrics.getBounds().width();
            screenHeight = deviceWindowMetrics.getBounds().height();

}

Log.d(TAG, "The screen width == " + screenWidth);

I can't figure out the reason why it prints 1080 as the width instead of 1440. The phone is a used phone I purchased online - the only reasoning I can come up with is that this phone is not really a Samsung Galaxy S20. All the other phones I'm using to test with return the expected width and height. Am I doing something wrong?


Solution

  • Hi I m not sure about this but try changing the resolution on your phone. https://www.technobezz.com/how-to-change-screen-resolution-on-samsung-galaxy-s20-ultra/