1

I'd like to get the size of the screen of the phones but it keeps giving me wrong values, I already used

WindowManager windowmanager = (WindowManager)      
getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowmanager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);

float deviceWidth = displayMetrics.widthPixels;
float deviceHeight = displayMetrics.ydpi;

I tried this code too :

Resources resources = getResources();
Configuration config = resources.getConfiguration();
DisplayMetrics dm = resources.getDisplayMetrics();
// Note, screenHeightDp isn't reliable
// (it seems to be too small by the height of the status bar),
// but we assume screenWidthDp is reliable.
// Note also, dm.widthPixels,dm.heightPixels aren't reliably pixels
// (they get confused when in screen compatibility mode, it seems),
// but we assume their ratio is correct.
double screenWidthInPixels = (double)config.screenWidthDp *dm.density;
double screenHeightInPixels = screenWidthInPixels * dm.heightPixels     / dm.widthPixels;
deviceWidth = (int)(screenWidthInPixels + .5);
deviceHeight = (int)(screenHeightInPixels + .5);

And also that :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    Point realSize = new Point();
    display.getRealSize(realSize);
    deviceWidth= realSize.x;
    deviceHeight = realSize.y;
}

But on my Samsung S7 on sdk 7.0 I got 1920x1080 that is wrong because on a S7 on sdk 6.0.1 I got 2560x1440 that is the real value. I tried a lot of solution but found nothing good.

Thanks

Komal12
  • 3,340
  • 4
  • 16
  • 25
Hugo OUKHAI
  • 29
  • 1
  • 4

5 Answers5

0

use this

  getWindowManager().getDefaultDisplay().getHeight();
    getWindowManager().getDefaultDisplay().getWidth();
Hitesh Gehlot
  • 1,307
  • 1
  • 15
  • 30
0

This will work for sure.

try { 
display.getRealSize(size);
 height = size.y; 
width=size.x;
} catch (NoSuchMethodError e) {
height = display.getHeight();
width=display.getWidth();
}
Saurav Prakash
  • 588
  • 1
  • 5
  • 26
0

Try this link

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Community
  • 1
  • 1
Ahmad Vatani
  • 1,630
  • 4
  • 21
  • 34
0

The method below return a Point that contain display size (x as Width and y as Height):

public static Point checkDisplaySize(Context context) {
        Point tempDisplaySize = new Point();
        try {
            WindowManager manager = (WindowManager) context().getSystemService(Context.WINDOW_SERVICE);
            if (manager != null) {
                Display display = manager.getDefaultDisplay();
                if (display != null) {
                    display.getSize(tempDisplaySize);
                    Log.d("tmessages", "display size = " + displaySize.x + " " + displaySize.y);
                }
            }
        } catch (Exception e) {
            Log.e("tmessages", e.getMessage(), e);
        }
        return tempDisplaySize;
    }

P.S: This is the Code Telegram uses to get display size.

Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36
  • I tried this method but got this log : tmessagesdisplay size = 1080 1920 435.42825x431.57477 – Hugo OUKHAI Apr 10 '17 at 09:53
  • well "1080 1920" is your device size in pixel – Keivan Esbati Apr 10 '17 at 09:56
  • But I know that 1920x1080 is wrong, cause if you check on Samsung site you'll find 2560x1440, value I found once but on a Samsung S7 on sdk 6.0.1 but since I updated it no way to find this value. And I tried on other phones I get wrong values too – Hugo OUKHAI Apr 10 '17 at 09:59
  • This value is the real state your phone get to draw Views so it is indeed all the pixel you get.. But It can be a OS related issue, do you think it is possible that OS downscale your display to FHD? did you tweak any display setting? – Keivan Esbati Apr 10 '17 at 10:02
  • I guess I didn't because I get same values on another S7 on 7.0 , I also tried on some other phones and on emulator , still giving me wrong values. – Hugo OUKHAI Apr 10 '17 at 10:05
0

Your code is correct. Just in case if you wondering why you get that values, it is because your phone will automatically set the default resolution size to 1920x1080 after updated to 7.0 (Nougat) to conserve the battery life. One of the new features in Nougat is display scaling option, where you can set your phone (in this case, S7) to 3 available modes (WQHD (2560x1440), FHD (1920x1080), and HD (1280x720)). Try go to Settings -> Display and change the settings to your needs. You can read more here: Galaxy S7 on Nougat defaults to 1080p

Amad Yus
  • 2,856
  • 1
  • 24
  • 32
  • Thanks, didn't saw that, I changed the settings and had the right result, but I still have problem on an emulated Nexus 5 updated to 7.1.1 , I'm getting 1794x1080 instead of 1920x1080, do you have any idea why ? – Hugo OUKHAI Apr 10 '17 at 11:15
  • Found my answer, the problem was that the button bar isn't regarded as a part of the screen. So if you create a virtual device with pysical buttons you won't have this error anymore. Same for real devices that use button bar, as the Huawei P9. – Hugo OUKHAI Apr 10 '17 at 12:31