Example usage for android.view Display getMetrics

List of usage examples for android.view Display getMetrics

Introduction

In this page you can find the example usage for android.view Display getMetrics.

Prototype

public void getMetrics(DisplayMetrics outMetrics) 

Source Link

Document

Gets display metrics that describe the size and density of this display.

Usage

From source file:Main.java

public static int[] getRealScreenSize(Context context) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);

    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
        try {/* www .j a va2  s  .c o  m*/
            widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
            heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
        } catch (Exception ignored) {
        }
    } else if (Build.VERSION.SDK_INT >= 17) {// includes window decorations (statusbar bar/menu bar)
        Point realSize = new Point();
        d.getRealSize(realSize);
        widthPixels = realSize.x;
        heightPixels = realSize.y;
    }

    size[0] = widthPixels;
    size[1] = heightPixels;
    return size;
}

From source file:Main.java

public static int[] getScreenSize(Context context, boolean useDeviceSize) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    if (!useDeviceSize) {
        size[0] = widthPixels;//from   w w  w  .j  ava2 s.c  o  m
        size[1] = heightPixels;
        return size;
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
        try {
            widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
            heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
        } catch (Exception ignored) {
        }
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17)
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
            widthPixels = realSize.x;
            heightPixels = realSize.y;
        } catch (Exception ignored) {
        }
    size[0] = widthPixels;
    size[1] = heightPixels;
    return size;
}

From source file:Main.java

public static String getResolution(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    if (wm == null)
        return null;

    Display display = wm.getDefaultDisplay();
    if (display == null)
        return null;

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics.widthPixels + "x" + metrics.heightPixels;
}

From source file:Main.java

public static int[] getScreenSize(Context context, boolean useDeviceSize) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    if (!useDeviceSize) {
        size[0] = widthPixels;//from  w w  w .j av  a  2  s. co m
        size[1] = heightPixels - getStatusBarHeight(context);

        return size;
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
        try {
            widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
            heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
        } catch (Exception ignored) {
        }
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17)
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
            widthPixels = realSize.x;
            heightPixels = realSize.y;
        } catch (Exception ignored) {
        }
    size[0] = widthPixels;
    size[1] = heightPixels;
    return size;
}

From source file:Main.java

public static DisplayMetrics getDisplayMetrics(Context context) {
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics;
}

From source file:Main.java

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static Point getFullDisplaySize(Context context) {
    Point point = new Point();

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    Display display = wm.getDefaultDisplay();
    Method mGetRawH = null, mGetRawW = null;

    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        display.getRealMetrics(outMetrics);
        point.x = outMetrics.widthPixels;
        point.y = outMetrics.heightPixels;
    } else {/*  www. j  a  va 2s. c  om*/
        try {
            mGetRawH = Display.class.getMethod("getRawHeight");
            mGetRawW = Display.class.getMethod("getRawWidth");
            point.x = (Integer) mGetRawW.invoke(display);
            point.y = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            display.getMetrics(outMetrics);
            point.x = display.getWidth();
            point.y = display.getHeight();
            e.printStackTrace();
        }
    }

    return point;
}

From source file:Main.java

public static float getDisplayDensity(Context context) {
    if (sDensity == Float.MAX_VALUE) {
        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();// w  w w.  j a  v a 2s.c  o  m
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        sDensity = metrics.density;
    }
    return sDensity;
}

From source file:com.activiti.android.ui.utils.DisplayUtils.java

public static int getWidth(Activity context) {
    Display display = context.getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    float density = context.getResources().getDisplayMetrics().density;
    int width = Math.round(outMetrics.widthPixels / density);

    Resources res = context.getResources();

    int coeff = 150;

    return coeff;
}

From source file:com.arlib.floatingsearchview.util.Util.java

public static int getScreenWidth(Activity activity) {

    Display display = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    return outMetrics.widthPixels;
}

From source file:org.acra.collector.DisplayManagerCollector.java

@NonNull
private static String collectMetrics(@NonNull Display display) {
    final DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return collectMetrics(display.getDisplayId() + ".metrics", metrics);
}