Example usage for android.util DisplayMetrics DisplayMetrics

List of usage examples for android.util DisplayMetrics DisplayMetrics

Introduction

In this page you can find the example usage for android.util DisplayMetrics DisplayMetrics.

Prototype

public DisplayMetrics() 

Source Link

Usage

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  w w  . java  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:Main.java

public static int getDeviceWidth(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.widthPixels;
}

From source file:Main.java

public static int getDeviceHeight(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.heightPixels;
}

From source file:Main.java

/**
 * Returns a valid DisplayMetrics object
 *
 * @param context valid context/* w ww . j a  v a2  s .c  o  m*/
 * @return DisplayMetrics object
 */
static DisplayMetrics getDisplayMetrics(final Context context) {
    final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    final DisplayMetrics metrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(metrics);
    return metrics;
}

From source file:Main.java

public static int[] getResolution(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return new int[] { dm.widthPixels, dm.heightPixels };
}

From source file:Main.java

public static int getScreenW(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = activity.getResources().getDisplayMetrics();
    int w = dm.widthPixels;
    // int w = aty.getWindowManager().getDefaultDisplay().getWidth();
    return w;/*from  w  w w. java  2 s . co m*/
}

From source file:Main.java

public static int getDialogW(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = activity.getResources().getDisplayMetrics();
    int w = dm.widthPixels - 100;
    // int w = aty.getWindowManager().getDefaultDisplay().getWidth() - 100;
    return w;/*from   w w w  . ja v a 2s  .  co m*/
}

From source file:Main.java

public static int getScreenH(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    dm = activity.getResources().getDisplayMetrics();
    int h = dm.heightPixels;
    // int h = aty.getWindowManager().getDefaultDisplay().getHeight();
    return h;//from  w w w.ja v a  2  s . c  o  m
}

From source file:Main.java

public static float getDensity(Activity context) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.density;
}

From source file:Main.java

public static int getPixelsWidth(Activity context) {
    DisplayMetrics dm = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.widthPixels;
}