Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screen resolution of device

I used following method to get the screen size:

public static Point getScreenSize(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int w = wm.getDefaultDisplay().getWidth();
    int h = wm.getDefaultDisplay().getHeight();
    return new Point(w, h);
}

On my samsung galaxy s6, this method returns 1080x1920... Although my device has a resolution of 1440x2560.

Why? Is there a better method to get the screen resolution that works on newer phones reliable as well?

EDIT

I need this method in a service! I don't have a view/activity for help, only the application context! And I need the REAL pixels

like image 868
prom85 Avatar asked Jul 27 '16 13:07

prom85


3 Answers

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;
like image 130
Lubomir Babev Avatar answered Nov 17 '22 18:11

Lubomir Babev


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.

like image 27
Domenico Avatar answered Nov 17 '22 16:11

Domenico


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 + "}";
}
like image 2
ViramP Avatar answered Nov 17 '22 17:11

ViramP