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:website.openeng.anki.Reviewer.java

@SuppressLint("NewApi")
@Override/* w w  w.  j  a va 2s  .  co m*/
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.reviewer, menu);
    Resources res = getResources();
    if (mCurrentCard != null && mCurrentCard.note().hasTag("marked")) {
        menu.findItem(R.id.action_mark_card).setTitle(R.string.menu_unmark_note)
                .setIcon(R.drawable.ic_star_white_24dp);
    } else {
        menu.findItem(R.id.action_mark_card).setTitle(R.string.menu_mark_note)
                .setIcon(R.drawable.ic_star_outline_white_24dp);
    }
    if (colIsOpen() && getCol().undoAvailable()) {
        menu.findItem(R.id.action_undo).setEnabled(true).getIcon().setAlpha(Themes.ALPHA_ICON_ENABLED_LIGHT);
    } else {
        menu.findItem(R.id.action_undo).setEnabled(false).getIcon().setAlpha(Themes.ALPHA_ICON_DISABLED_LIGHT);
    }
    if (mPrefWhiteboard) {
        // Don't force showing mark icon when whiteboard enabled
        // TODO: allow user to customize which icons are force-shown
        MenuItemCompat.setShowAsAction(menu.findItem(R.id.action_mark_card),
                MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
        // Check if we can forceably squeeze in 3 items into the action bar, if not hide "show whiteboard"
        if (CompatHelper.getSdkVersion() >= 14 && !ViewConfiguration.get(this).hasPermanentMenuKey()) {
            // Android 4.x device with overflow menu in the action bar and small screen can't
            // support forcing 2 extra items into the action bar
            Display display = getWindowManager().getDefaultDisplay();
            DisplayMetrics outMetrics = new DisplayMetrics();
            display.getMetrics(outMetrics);
            float density = getResources().getDisplayMetrics().density;
            float dpWidth = outMetrics.widthPixels / density;
            if (dpWidth < 360) {
                menu.findItem(R.id.action_hide_whiteboard).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
            }
        }
        // Configure the whiteboard related items in the action bar
        menu.findItem(R.id.action_enable_whiteboard).setTitle(R.string.disable_whiteboard);
        menu.findItem(R.id.action_hide_whiteboard).setVisible(true);
        menu.findItem(R.id.action_clear_whiteboard).setVisible(true);

        Drawable whiteboardIcon = getResources().getDrawable(R.drawable.ic_gesture_white_24dp);
        if (mShowWhiteboard) {
            whiteboardIcon.setAlpha(255);
            menu.findItem(R.id.action_hide_whiteboard).setIcon(whiteboardIcon);
            menu.findItem(R.id.action_hide_whiteboard).setTitle(R.string.hide_whiteboard);
        } else {
            whiteboardIcon.setAlpha(77);
            menu.findItem(R.id.action_hide_whiteboard).setIcon(whiteboardIcon);
            menu.findItem(R.id.action_hide_whiteboard).setTitle(R.string.show_whiteboard);
        }
    } else {
        menu.findItem(R.id.action_enable_whiteboard).setTitle(R.string.enable_whiteboard);
    }
    if (!CompatHelper.isHoneycomb() && !mDisableClipboard) {
        menu.findItem(R.id.action_search_dictionary).setVisible(true)
                .setEnabled(!(mPrefWhiteboard && mShowWhiteboard))
                .setTitle(clipboardHasText() ? Lookup.getSearchStringTitle()
                        : res.getString(R.string.menu_select));
    }
    return super.onCreateOptionsMenu(menu);
}

From source file:com.ichi2.anki.Reviewer.java

@SuppressLint("NewApi")
@Override/*from w  ww.ja  v  a  2s  .  c  o  m*/
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.reviewer, menu);
    Resources res = getResources();
    if (mCurrentCard != null && mCurrentCard.note().hasTag("marked")) {
        menu.findItem(R.id.action_mark_card).setTitle(R.string.menu_unmark_note)
                .setIcon(R.drawable.ic_star_white_24dp);
    } else {
        menu.findItem(R.id.action_mark_card).setTitle(R.string.menu_mark_note)
                .setIcon(R.drawable.ic_star_outline_white_24dp);
    }
    if (colIsOpen() && getCol().undoAvailable()) {
        menu.findItem(R.id.action_undo).setEnabled(true).getIcon().setAlpha(Themes.ALPHA_ICON_ENABLED_LIGHT);
    } else {
        menu.findItem(R.id.action_undo).setEnabled(false).getIcon().setAlpha(Themes.ALPHA_ICON_DISABLED_LIGHT);
    }
    if (mPrefWhiteboard) {
        // Don't force showing mark icon when whiteboard enabled
        // TODO: allow user to customize which icons are force-shown
        MenuItemCompat.setShowAsAction(menu.findItem(R.id.action_mark_card),
                MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
        // Check if we can forceably squeeze in 3 items into the action bar, if not hide "show whiteboard"
        if (CompatHelper.getSdkVersion() >= 14 && !ViewConfiguration.get(this).hasPermanentMenuKey()) {
            // Android 4.x device with overflow menu in the action bar and small screen can't
            // support forcing 2 extra items into the action bar
            Display display = getWindowManager().getDefaultDisplay();
            DisplayMetrics outMetrics = new DisplayMetrics();
            display.getMetrics(outMetrics);
            float density = getResources().getDisplayMetrics().density;
            float dpWidth = outMetrics.widthPixels / density;
            if (dpWidth < 360) {
                menu.findItem(R.id.action_hide_whiteboard).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
            }
        }
        // Configure the whiteboard related items in the action bar
        menu.findItem(R.id.action_enable_whiteboard).setTitle(R.string.disable_whiteboard);
        menu.findItem(R.id.action_hide_whiteboard).setVisible(true);
        menu.findItem(R.id.action_clear_whiteboard).setVisible(true);

        Drawable whiteboardIcon = getResources().getDrawable(R.drawable.ic_gesture_white_24dp);
        if (mShowWhiteboard) {
            whiteboardIcon.setAlpha(255);
            menu.findItem(R.id.action_hide_whiteboard).setIcon(whiteboardIcon);
            menu.findItem(R.id.action_hide_whiteboard).setTitle(R.string.hide_whiteboard);
        } else {
            whiteboardIcon.setAlpha(77);
            menu.findItem(R.id.action_hide_whiteboard).setIcon(whiteboardIcon);
            menu.findItem(R.id.action_hide_whiteboard).setTitle(R.string.show_whiteboard);
        }
    } else {
        menu.findItem(R.id.action_enable_whiteboard).setTitle(R.string.enable_whiteboard);
    }
    if (!CompatHelper.isHoneycomb() && !mDisableClipboard) {
        menu.findItem(R.id.action_search_dictionary).setVisible(true)
                .setEnabled(!(mPrefWhiteboard && mShowWhiteboard))
                .setTitle(clipboardHasText() ? Lookup.getSearchStringTitle()
                        : res.getString(R.string.menu_select));
    }
    if (getCol().getDecks().isDyn(getParentDid())) {
        menu.findItem(R.id.action_open_deck_options).setVisible(false);
    }
    return super.onCreateOptionsMenu(menu);
}

From source file:kr.wdream.storyshop.AndroidUtilities.java

public static void checkDisplaySize(Context context, Configuration newConfiguration) {
    try {//from   w  w  w  . ja  va 2s. c o  m
        density = context.getResources().getDisplayMetrics().density;
        Configuration configuration = newConfiguration;
        if (configuration == null) {
            configuration = context.getResources().getConfiguration();
        }
        usingHardwareInput = configuration.keyboard != Configuration.KEYBOARD_NOKEYS
                && configuration.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (manager != null) {
            Display display = manager.getDefaultDisplay();
            if (display != null) {
                display.getMetrics(displayMetrics);
                display.getSize(displaySize);
            }
        }
        if (configuration.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
            int newSize = (int) Math.ceil(configuration.screenWidthDp * density);
            if (Math.abs(displaySize.x - newSize) > 3) {
                displaySize.x = newSize;
            }
        }
        if (configuration.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
            int newSize = (int) Math.ceil(configuration.screenHeightDp * density);
            if (Math.abs(displaySize.y - newSize) > 3) {
                displaySize.y = newSize;
            }
        }
        FileLog.e("tmessages", "display size = " + displaySize.x + " " + displaySize.y + " "
                + displayMetrics.xdpi + "x" + displayMetrics.ydpi);
    } catch (Exception e) {
        FileLog.e("tmessages", e);
    }
}

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

/**
 * ??//  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.nextgis.mobile.fragment.AttributesFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mLayer == null) {
        getActivity().getSupportFragmentManager().popBackStack();
        Toast.makeText(getContext(), R.string.error_layer_not_inited, Toast.LENGTH_SHORT).show();
        return null;
    }//w  w w  . jav a 2s. co m

    getActivity().setTitle(mLayer.getName());
    setHasOptionsMenu(!isTablet());

    int resId = isTablet() ? R.layout.fragment_attributes_tab : R.layout.fragment_attributes;
    View view = inflater.inflate(resId, container, false);

    if (isTablet()) {
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(view.getLayoutParams());
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        lp.width = metrics.widthPixels / 2;

        int[] attrs = { R.attr.actionBarSize };
        TypedArray ta = getActivity().obtainStyledAttributes(attrs);
        lp.bottomMargin = ta.getDimensionPixelSize(0, 0);
        ta.recycle();

        view.setLayoutParams(lp);
    }

    mAttributes = (LinearLayout) view.findViewById(R.id.ll_attributes);
    return view;
}

From source file:com.androchill.call411.MainActivity.java

private Phone getHardwareSpecs() {
    ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    PhoneBuilder pb = new PhoneBuilder();
    pb.setModelNumber(Build.MODEL);/*from w w w .j a  va2  s.  co m*/
    pb.setManufacturer(Build.MANUFACTURER);

    Process p = null;
    String board_platform = "No data available";
    try {
        p = new ProcessBuilder("/system/bin/getprop", "ro.chipname").redirectErrorStream(true).start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while ((line = br.readLine()) != null) {
            board_platform = line;
        }
        p.destroy();
    } catch (IOException e) {
        e.printStackTrace();
        board_platform = "No data available";
    }

    pb.setProcessor(board_platform);
    ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo();
    activityManager.getMemoryInfo(mInfo);
    pb.setRam((int) (mInfo.totalMem / 1048576L));
    pb.setBatteryCapacity(getBatteryCapacity());
    pb.setTalkTime(-1);
    pb.setDimensions("No data available");

    WindowManager mWindowManager = getWindowManager();
    Display mDisplay = mWindowManager.getDefaultDisplay();
    Point mPoint = new Point();
    mDisplay.getSize(mPoint);
    pb.setScreenResolution(mPoint.x + " x " + mPoint.y + " pixels");

    DisplayMetrics mDisplayMetrics = new DisplayMetrics();
    mDisplay.getMetrics(mDisplayMetrics);
    int mDensity = mDisplayMetrics.densityDpi;
    pb.setScreenSize(
            Math.sqrt(Math.pow(mPoint.x / (double) mDensity, 2) + Math.pow(mPoint.y / (double) mDensity, 2)));

    Camera camera = Camera.open(0);
    android.hardware.Camera.Parameters params = camera.getParameters();
    List sizes = params.getSupportedPictureSizes();
    Camera.Size result = null;

    ArrayList<Integer> arrayListForWidth = new ArrayList<Integer>();
    ArrayList<Integer> arrayListForHeight = new ArrayList<Integer>();

    for (int i = 0; i < sizes.size(); i++) {
        result = (Camera.Size) sizes.get(i);
        arrayListForWidth.add(result.width);
        arrayListForHeight.add(result.height);
    }
    if (arrayListForWidth.size() != 0 && arrayListForHeight.size() != 0) {
        pb.setCameraMegapixels(
                Collections.max(arrayListForHeight) * Collections.max(arrayListForWidth) / (double) 1000000);
    } else {
        pb.setCameraMegapixels(-1);
    }
    camera.release();

    pb.setPrice(-1);
    pb.setWeight(-1);
    pb.setSystem("Android " + Build.VERSION.RELEASE);

    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    StatFs statInternal = new StatFs(Environment.getRootDirectory().getAbsolutePath());
    double storageSize = ((stat.getBlockSizeLong() * stat.getBlockCountLong())
            + (statInternal.getBlockSizeLong() * statInternal.getBlockCountLong())) / 1073741824L;
    pb.setStorageOptions(storageSize + " GB");
    TelephonyManager telephonyManager = ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE));
    String operatorName = telephonyManager.getNetworkOperatorName();
    if (operatorName.length() == 0)
        operatorName = "No data available";
    pb.setCarrier(operatorName);
    pb.setNetworkFrequencies("No data available");
    pb.setImage(null);
    return pb.createPhone();
}

From source file:ua.com.spacetv.mycookbook.fragments.FragTextRecipe.java

private void getDisplayMetrics() {
    Display display = getActivity().getWindowManager().getDefaultDisplay();
    android.util.DisplayMetrics metrics = new android.util.DisplayMetrics();
    display.getMetrics(metrics);
    displayWidth = metrics.widthPixels;//  ww  w. j  av a2 s  .c  o  m
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static void checkDisplaySize(Context context, Configuration newConfiguration) {
    try {/*from  w w w .  j a v  a  2  s.  c  o m*/
        density = context.getResources().getDisplayMetrics().density;
        Configuration configuration = newConfiguration;
        if (configuration == null) {
            configuration = context.getResources().getConfiguration();
        }
        usingHardwareInput = configuration.keyboard != Configuration.KEYBOARD_NOKEYS
                && configuration.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (manager != null) {
            Display display = manager.getDefaultDisplay();
            if (display != null) {
                display.getMetrics(displayMetrics);
                display.getSize(displaySize);
            }
        }
        if (configuration.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
            int newSize = (int) Math.ceil(configuration.screenWidthDp * density);
            if (Math.abs(displaySize.x - newSize) > 3) {
                displaySize.x = newSize;
            }
        }
        if (configuration.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
            int newSize = (int) Math.ceil(configuration.screenHeightDp * density);
            if (Math.abs(displaySize.y - newSize) > 3) {
                displaySize.y = newSize;
            }
        }
        FileLog.e("display size = " + displaySize.x + " " + displaySize.y + " " + displayMetrics.xdpi + "x"
                + displayMetrics.ydpi);
    } catch (Exception e) {
        FileLog.e(e);
    }
}

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

/**
 * If there is a virtual buttons, may have a miscalculation
 *
 * @return/*from   www.  j a v  a 2 s  .  c o 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:org.artoolkit.ar.samples.ARMovie.ARMovieActivity.java

private void updateNativeDisplayParameters() {
    Display d = getWindowManager().getDefaultDisplay();
    int orientation = d.getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    d.getMetrics(dm);
    int w = dm.widthPixels;
    int h = dm.heightPixels;
    int dpi = dm.densityDpi;
    nativeDisplayParametersChanged(orientation, w, h, dpi);
}