1

I am getting display size using this function but i get wrong display size in this(moto G2,HTC E8 ,Moto E,Lg G2) device. but in my other device i got perfect size. how to I calculate this size?

 private void getDefaultDisplay(WindowManager wm) {
    WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    display.getSize(size);
    CAMERA_WIDTH = size.x;
    CAMERA_HEIGHT = size.y;
    Log.d("Display", CAMERA_WIDTH + ":" + CAMERA_HEIGHT);
    } else {
    CAMERA_WIDTH = display.getWidth(); // deprecated
    CAMERA_HEIGHT = display.getHeight();
    }
    }

device Name calcualted size : original size

moto G2 720x1184 : 720x1280

HTC E8 1080x1776 : 1080x1920

Moto E 540x888 : 540x960

LG G2 1080x1776 : 1080 x1920

Mr X
  • 1,053
  • 2
  • 11
  • 24

2 Answers2

1

Its easier and nicer to use :

int displayWidth = getResources().getDisplayMetrics().widthPixels;

and

int displayHeight = getResources().getDisplayMetrics().heightPixels;
A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38
0

Try to use this code, i used this in my apps and (in my experience) it always return correct value :

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129