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:com.dunrite.xpaper.utility.Utils.java

/**
 * Gets Device's screen resolution width
 *
 * @param context current context//  w w w .  ja v a  2s. co m
 * @return width value
 * //TODO: get actual value, if possible
 */
public static int getDeviceResWidth(Context context) {
    int width = 0;
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);
    Display display = wm.getDefaultDisplay();
    int density = metrics.densityDpi;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        display.getRealMetrics(metrics);

        width = metrics.widthPixels;
    }
    Log.d("DEVICE WIDTH", "" + width);
    return width;
}

From source file:com.farmerbb.secondscreen.util.U.java

public static boolean runDensityCommand(Context context, String requestedDpi) {
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display disp = wm.getDefaultDisplay();
    disp.getRealMetrics(metrics);

    SharedPreferences prefMain = getPrefMain(context);
    String currentDpi;/*from  w w  w  . ja  v a 2  s .c  om*/
    String nativeDpi = Integer
            .toString(SystemProperties.getInt("ro.sf.lcd_density", prefMain.getInt("density", 0)));

    if (prefMain.getBoolean("debug_mode", false)) {
        SharedPreferences prefCurrent = getPrefCurrent(context);
        currentDpi = prefCurrent.getString("density", "reset");

        if ("reset".equals(currentDpi))
            currentDpi = nativeDpi;
    } else
        currentDpi = Integer.toString(metrics.densityDpi);

    if (requestedDpi.equals("reset"))
        requestedDpi = nativeDpi;

    return !requestedDpi.equals(currentDpi);
}

From source file:com.farmerbb.secondscreen.util.U.java

public static boolean runSizeCommand(Context context, String requestedRes) {
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display disp = wm.getDefaultDisplay();
    disp.getRealMetrics(metrics);

    SharedPreferences prefMain = getPrefMain(context);
    String currentRes = " ";
    String nativeRes;//from  w  w w.  j  ava  2 s  . c  om

    if (prefMain.getBoolean("landscape", false))
        nativeRes = Integer.toString(prefMain.getInt("height", 0)) + "x"
                + Integer.toString(prefMain.getInt("width", 0));
    else
        nativeRes = Integer.toString(prefMain.getInt("width", 0)) + "x"
                + Integer.toString(prefMain.getInt("height", 0));

    if (prefMain.getBoolean("debug_mode", false)) {
        SharedPreferences prefCurrent = getPrefCurrent(context);
        currentRes = prefCurrent.getString("size", "reset");

        if ("reset".equals(currentRes))
            currentRes = nativeRes;
    } else {
        if ((context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT
                && !prefMain.getBoolean("landscape", false))
                || (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
                        && prefMain.getBoolean("landscape", false))) {
            currentRes = Integer.toString(metrics.widthPixels) + "x" + Integer.toString(metrics.heightPixels);
        } else if ((context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
                && !prefMain.getBoolean("landscape", false))
                || (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT
                        && prefMain.getBoolean("landscape", false))) {
            currentRes = Integer.toString(metrics.heightPixels) + "x" + Integer.toString(metrics.widthPixels);
        }
    }

    if (requestedRes.equals("reset"))
        requestedRes = nativeRes;

    return !requestedRes.equals(currentRes);
}

From source file:org.xwalk.runtime.extension.api.device_capabilities.DeviceCapabilitiesDisplay.java

public JSONObject convertDisplayToJSON(Display disp) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    disp.getRealMetrics(displayMetrics);

    Point realSize = new Point();
    disp.getRealSize(realSize);//from   www  . j  a  v  a  2  s.co m

    Point availSize = new Point();
    disp.getSize(availSize);

    JSONObject out = new JSONObject();
    try {
        out.put("id", disp.getDisplayId());
        out.put("name", disp.getName());
        out.put("isPrimary", disp.getDisplayId() == disp.DEFAULT_DISPLAY);
        out.put("isInternal", disp.getDisplayId() == disp.DEFAULT_DISPLAY);
        out.put("dpiX", (int) displayMetrics.xdpi);
        out.put("dpiY", (int) displayMetrics.ydpi);
        out.put("width", realSize.x);
        out.put("height", realSize.y);
        out.put("availWidth", availSize.x);
        out.put("availHeight", availSize.y);
    } catch (JSONException e) {
        return mDeviceCapabilities.setErrorMessage(e.toString());
    }
    return out;
}

From source file:org.chromium.ChromeSystemDisplay.java

private JSONObject getBounds(final Display display) throws JSONException {
    JSONObject ret = new JSONObject();
    int widthPixels = 0;
    int heightPixels = 0;
    final DisplayMetrics displayMetrics = new DisplayMetrics();

    try {/*from  w w w  .j  a v  a2s .c om*/
        display.getRealMetrics(displayMetrics);
    } catch (NoSuchMethodError e) {
        display.getMetrics(displayMetrics);
    }
    widthPixels = displayMetrics.widthPixels;
    heightPixels = displayMetrics.heightPixels;

    ret.put("left", 0);
    ret.put("top", 0);
    ret.put("width", widthPixels);
    ret.put("height", heightPixels);

    return ret;
}

From source file:mobi.cangol.mobile.navigation.DrawerMenuLayout.java

/**
 * ??//from w  w  w.j a v  a2 s.c o  m
 * @return
 */
private boolean checkDeviceHasNavigationBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        display.getRealMetrics(realDisplayMetrics);
        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;
        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    } else {
        boolean hasNavigationBar = false;
        Resources resources = getContext().getResources();
        int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = resources.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                hasNavigationBar = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hasNavigationBar;
    }
}

From source file:com.smartcodeunited.demo.bluetooth.activity.BaseActivity.java

/**
 * If there is a virtual buttons, may have a miscalculation
 *
 * @return/*from   w w  w.  j ava 2 s.co m*/
 */
private boolean hasNavigationBar() {
    boolean hasMenuKey = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        hasMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display d = 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;
        return realWidth > displayWidth || realHeight > displayHeight;
    }

    boolean hasPhysicsBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    boolean hasPhysicsHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
    if (hasPhysicsBackKey && hasPhysicsHomeKey) {
        return false;
    } else {
        return true;
    }
}

From source file:com.mruddy.devdataviewer.DevDataListFragment.java

/**
 * @return/* ww w . j  av a2  s  .  c o m*/
 */
@SuppressLint("NewApi")
private List<String> getDevDataList() {
    final List<String> result = new LinkedList<String>();
    final Display display = this.getActivity().getWindowManager().getDefaultDisplay();
    final DisplayMetrics appAreaDisplayMetrics = new DisplayMetrics();
    display.getMetrics(appAreaDisplayMetrics);
    result.add("API Level: " + Build.VERSION.SDK_INT);
    result.add("Android Release: " + Build.VERSION.RELEASE);
    if (Build.VERSION.SDK_INT >= 17) {
        final DisplayMetrics fullDisplayMetrics = new DisplayMetrics();
        display.getRealMetrics(fullDisplayMetrics);
        result.add("Full Width (px): " + fullDisplayMetrics.widthPixels);
        result.add("Full Height (px): " + fullDisplayMetrics.heightPixels);
        result.add("Full Width (dp): " + ((int) (fullDisplayMetrics.widthPixels / fullDisplayMetrics.density)));
        result.add(
                "Full Height (dp): " + ((int) (fullDisplayMetrics.heightPixels / fullDisplayMetrics.density)));
    }
    result.add("App Area Width (px): " + appAreaDisplayMetrics.widthPixels);
    result.add("App Area Height (px): " + appAreaDisplayMetrics.heightPixels);
    result.add("App Area Width (dp): "
            + ((int) (appAreaDisplayMetrics.widthPixels / appAreaDisplayMetrics.density)));
    result.add("App Area Height (dp): "
            + ((int) (appAreaDisplayMetrics.heightPixels / appAreaDisplayMetrics.density)));
    result.add("dp Scaling Factor: " + appAreaDisplayMetrics.density);
    final String densityBucket = DevDataListFragment.DENSITY_BUCKETS.get(appAreaDisplayMetrics.densityDpi);
    result.add("Density Bucket: " + (null != densityBucket ? densityBucket : "other") + " ("
            + appAreaDisplayMetrics.densityDpi + ")");
    result.add("Actual xdpi: " + appAreaDisplayMetrics.xdpi);
    result.add("Actual ydpi: " + appAreaDisplayMetrics.ydpi);
    if (Build.VERSION.SDK_INT >= 8) {
        final String rotation = DevDataListFragment.ROTATION.get(display.getRotation());
        result.add("Rotation: " + (null != rotation ? rotation : "other (" + display.getRotation() + ")"));
    }
    final String orientation = DevDataListFragment.ORIENTATION
            .get(this.getResources().getConfiguration().orientation);
    result.add("Orientation: " + (null != orientation ? orientation
            : "other (" + this.getResources().getConfiguration().orientation + ")"));
    final int screenSizeKey = this.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK;
    final String screenSizeBucket = DevDataListFragment.SCREEN_SIZE_BUCKETS.get(screenSizeKey);
    result.add(
            "Size Bucket: " + (null != screenSizeBucket ? screenSizeBucket : "other (" + screenSizeKey + ")"));
    return result;
}

From source file:com.example.ray.firstapp.bottombar.BottomBar.java

private static void navBarMagic(Activity activity, final BottomBar bottomBar) {
    Resources res = activity.getResources();
    int softMenuIdentifier = res.getIdentifier("config_showNavigationBar", "bool", "android");
    int navBarIdentifier = res.getIdentifier("navigation_bar_height", "dimen", "android");
    int navBarHeight = 0;

    if (navBarIdentifier > 0) {
        navBarHeight = res.getDimensionPixelSize(navBarIdentifier);
    }/*from ww  w. j av  a  2s. co  m*/

    if (!bottomBar.drawBehindNavBar() || navBarHeight == 0
            || (!(softMenuIdentifier > 0 && res.getBoolean(softMenuIdentifier)))) {
        return;
    }

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

    /**
     * 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;
        }
    }
    /**
     * End of delicious copy-paste code
     */

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
            && res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        activity.getWindow().getAttributes().flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;

        if (bottomBar.useTopOffset()) {
            int offset;
            int statusBarResource = res.getIdentifier("status_bar_height", "dimen", "android");

            if (statusBarResource > 0) {
                offset = res.getDimensionPixelSize(statusBarResource);
            } else {
                offset = MiscUtils.dpToPixel(activity, 25);
            }

            if (!bottomBar.useOnlyStatusbarOffset()) {
                TypedValue tv = new TypedValue();
                if (activity.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                    offset += TypedValue.complexToDimensionPixelSize(tv.data, res.getDisplayMetrics());
                } else {
                    offset += MiscUtils.dpToPixel(activity, 56);
                }
            }

            bottomBar.getUserContainer().setPadding(0, offset, 0, 0);
        }

        final View outerContainer = bottomBar.getOuterContainer();
        final int navBarHeightCopy = navBarHeight;
        bottomBar.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @SuppressWarnings("deprecation")
                    @Override
                    public void onGlobalLayout() {
                        bottomBar.shyHeightAlreadyCalculated();

                        int newHeight = outerContainer.getHeight() + navBarHeightCopy;
                        outerContainer.getLayoutParams().height = newHeight;

                        if (bottomBar.isShy()) {
                            int defaultOffset = bottomBar.useExtraOffset() ? navBarHeightCopy : 0;
                            bottomBar.setTranslationY(defaultOffset);
                            ((CoordinatorLayout.LayoutParams) bottomBar.getLayoutParams())
                                    .setBehavior(new BottomNavigationBehavior(newHeight, defaultOffset));
                        }

                        ViewTreeObserver obs = outerContainer.getViewTreeObserver();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                            obs.removeOnGlobalLayoutListener(this);
                        } else {
                            obs.removeGlobalOnLayoutListener(this);
                        }
                    }
                });
    }
}

From source file:com.tomer.alwayson.SettingsFragment.java

private boolean hasSoftKeys() {
    boolean hasSoftwareKeys;

    Display d = getActivity().getWindowManager().getDefaultDisplay();

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

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

    DisplayMetrics displayMetrics = new DisplayMetrics();
    d.getMetrics(displayMetrics);//from   ww w.ja v a 2s . com

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

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

    prefs.setBool(Prefs.KEYS.HAS_SOFT_KEYS.toString(), hasSoftwareKeys);

    return hasSoftwareKeys;
}