Example usage for android.view Display getRealMetrics

List of usage examples for android.view Display getRealMetrics

Introduction

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

Prototype

public void getRealMetrics(DisplayMetrics outMetrics) 

Source Link

Document

Gets display metrics based on the real size of this display.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static int getScreenRealH(Context context) {
    int h;/*from  w w  w.java2 s.co  m*/
    WindowManager winMgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = winMgr.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    if (Build.VERSION.SDK_INT >= 17) {
        display.getRealMetrics(dm);
        h = dm.heightPixels;
    } else {
        try {
            Method method = Class.forName("android.view.Display").getMethod("getRealMetrics",
                    DisplayMetrics.class);
            method.invoke(display, dm);
            h = dm.heightPixels;
        } catch (Exception e) {
            display.getMetrics(dm);
            h = dm.heightPixels;
        }
    }
    return h;
}

From source file:Main.java

public static Point getSize(Display display) {
    if (Build.VERSION.SDK_INT >= 17) {
        Point outPoint = new Point();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        outPoint.x = metrics.widthPixels;
        outPoint.y = metrics.heightPixels;
        return outPoint;
    }/*ww w  .java2s  .  c  o m*/
    if (Build.VERSION.SDK_INT >= 14) {
        Point outPoint = getRealSize(display);
        if (outPoint != null)
            return outPoint;
    }
    Point outPoint = new Point();
    if (Build.VERSION.SDK_INT >= 13) {
        display.getSize(outPoint);
    } else {
        outPoint.x = display.getWidth();
        outPoint.y = display.getHeight();
    }
    return outPoint;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Point getScreenRawSize(Display display) {
    if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
        Point outPoint = new Point();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        outPoint.x = metrics.widthPixels;
        outPoint.y = metrics.heightPixels;
        return outPoint;
    } else {/*from  w w w.  jav a  2s . c  om*/
        Point outPoint = new Point();
        Method mGetRawH;
        try {
            mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            outPoint.x = (Integer) mGetRawW.invoke(display);
            outPoint.y = (Integer) mGetRawH.invoke(display);
            return outPoint;
        } catch (Throwable e) {
            return new Point(0, 0);
        }
    }
}

From source file:despotoski.nikola.github.com.bottomnavigationlayout.Util.java

public static boolean isNavigationBarTranslucent(Context context) {
    AppCompatActivity activity = (AppCompatActivity) context;
    getNavigationBarHeight(context);/*from   w ww . j  a  va2s  .c  om*/

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH
            && ViewConfiguration.get(activity).hasPermanentMenuKey()) {
        return false;
    }

    /**
     * Copy-paste coding made possible by:
     * http://stackoverflow.com/a/14871974/940036
     */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display d = activity.getWindowManager().getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        d.getRealMetrics(realDisplayMetrics);

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        boolean hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;

        if (!hasSoftwareKeys) {
            return hasSoftwareKeys;
        }
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return (activity.getWindow().getAttributes().flags
                & WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) != 0;
    } else {
        return false;
    }
}

From source file:Main.java

public static int getFullScreenSizeHeight(Context context) {
    int realWidth;
    int realHeight;

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    Point point = new Point();

    if (Build.VERSION.SDK_INT >= 17) {
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
        realHeight = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {/* w  ww  . j a  v a 2  s  .  co m*/
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
            realHeight = display.getHeight();
            Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
        realHeight = display.getHeight();
    }
    return realHeight;
}

From source file:Main.java

public static int getFullScreenSizeWidth(Context context) {
    int realWidth;
    int realHeight;

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    Point point = new Point();

    if (Build.VERSION.SDK_INT >= 17) {
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
        realHeight = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {/*from   w  w  w. jav  a2s  .  c om*/
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
            realHeight = display.getHeight();
            Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
        realHeight = display.getHeight();
    }
    return realWidth;
}

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);//w  ww  . j av a 2  s  . c om

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        display.getRealMetrics(outMetrics);
        point.x = outMetrics.widthPixels;
        point.y = outMetrics.heightPixels;
    } else {
        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:org.acra.collector.DisplayManagerCollector.java

@NonNull
private static String collectRealMetrics(@NonNull Display display) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        final DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        return collectMetrics(display.getDisplayId() + ".realMetrics", metrics);
    }/*from   w w  w.j av  a  2  s  .  c  o  m*/
    return "";
}

From source file:me.wimanacra.collector.DisplayManagerCollector.java

private static void collectRealMetrics(@NonNull Display display, JSONObject container) throws JSONException {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        final DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        JSONObject result = new JSONObject();
        collectMetrics(metrics, result);
        container.put("realMetrics", result);
    }//from w  w w .j  a  v a  2s  . co m
}

From source file:com.waz.zclient.utils.ViewUtils.java

public static int getRealDisplayWidth(Context context) {
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int realWidth;

    if (Build.VERSION.SDK_INT >= 17) {
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {//from w w w .  j  a  v a 2 s.  co  m
            Method getRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) getRawW.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
        }
    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
    }
    return realWidth;
}