Function that returns the screen resolution


I need a Java function that returns the resolution of my Android device.

asked by anonymous 05.02.2015 / 19:01

2 answers


You can use this way from API 13:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
int width = size.x;
int height = size.y;

For previous versions:

DisplayMetrics displaymetrics = new DisplayMetrics();
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;

You need to be in Activity to use this way, but it probably is. If it is not, the code will need to be changed to get the context.

05.02.2015 / 19:06

You can use DisplayMetrics :

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
int widht = displayMetrics.widthPixels;

Note: heightPixels returns the total size of your screen along with its status bar. If you want to ignore the size of the bar status (useful for operations of animations etc), you simply:

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    return result;
05.02.2015 / 19:05