Get screen resolution of device

前提是你 提交于 2019-12-05 10:20:17

The best way to get your screen resolution is from DisplayMetrics :

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;

If you are targeting for API >= 17, try with the getRealSize method of the Display class:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
wm.getDefaultDisplay().getRealSize(size);
String resolution = size.x + "x" + size.y;

for me, it worked.

Use this one:

private static String getScreenResolution(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    return "{" + width + "," + height + "}";
}

The API returns the number of pixels an app can use, so this is the correct result.

Use this.

DisplayMetrics metrics; 
int width = 0, height = 0;

In your onCreate method.

 metrics = new DisplayMetrics();        
 getWindowManager().getDefaultDisplay().getMetrics(metrics);


 height = metrics.heightPixels;     
 width = metrics.widthPixels;
Quantum4U

Try this:

 Display mDisplay = context.getWindowManager().getDefaultDisplay();

 mDisplay.getWidth();
 mDisplay.getHeight();

Note that this code uses deprecated APIs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!