Example usage for android.view WindowManager getDefaultDisplay

List of usage examples for android.view WindowManager getDefaultDisplay

Introduction

In this page you can find the example usage for android.view WindowManager getDefaultDisplay.

Prototype

public Display getDefaultDisplay();

Source Link

Document

Returns the Display upon which this WindowManager instance will create new windows.

Usage

From source file:Main.java

/**
 * Returns DisplayMetrics.//from www. j  a v  a 2  s.  com
 * 
 * @param context
 * @return
 */
public static DisplayMetrics getDisplayMetrics(Context context) {
    WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    window.getDefaultDisplay().getMetrics(outMetrics);

    return outMetrics;
}

From source file:Main.java

public static int getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.widthPixels;
}

From source file:Main.java

public static int getScreenHeight(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    return outMetrics.heightPixels;
}

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);/*from   w  w  w.j  a  va  2s  .co  m*/

    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 DisplayMetrics getDisplayMetrics(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics displayMetrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics;
}

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);//  w  w  w .  j  av a2s  . c om
    return metrics.widthPixels + "x" + metrics.heightPixels;
}

From source file:Main.java

public static HashMap<String, Integer> parseImageSizeWithUrl(Activity activity, String imageUrl) {

    int widthPx = DEFAULT_POST_IMAGE_WIDTH;
    int heightPx = DEFAULT_POST_IMAGE_HEIGHT;

    String[] file = imageUrl.split("\\.");
    int index = file.length - 2;
    String[] snippet = file[index].split("_");

    int snippetCount = snippet.length;
    if (snippetCount >= 3) {

        if (null != snippet[1]) {
            widthPx = Integer.parseInt(snippet[1]);
        }/*from w w  w.ja  v a  2 s  .  co m*/

        if (null != snippet[2]) {
            heightPx = Integer.parseInt(snippet[2]);
        }
    }
    WindowManager windowManager = activity.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();
    int screenWidth = 0;
    int screenHeight = 0;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(size);
        screenWidth = size.x;
        screenHeight = size.y;
    } else {
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
    }

    if (widthPx > screenWidth) {
        widthPx = screenWidth;
    } else if (widthPx < DEFAULT_POST_IMAGE_WIDTH) {
        widthPx = DEFAULT_POST_IMAGE_WIDTH;
    }

    if (heightPx > screenHeight) {
        heightPx = screenHeight;
    } else if (heightPx < DEFAULT_POST_IMAGE_HEIGHT) {
        heightPx = DEFAULT_POST_IMAGE_HEIGHT;
    }

    HashMap<String, Integer> imageSize = new HashMap<String, Integer>();
    imageSize.put("widthPx", widthPx);
    imageSize.put("heightPx", heightPx);
    return imageSize;
}

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 {//from   w  ww  .  ja v  a  2 s  .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 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 {//  w  ww. jav  a2s. c  o  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 realWidth;
}

From source file:Main.java

/**
 * Calculate displey size./* w ww  .  jav  a2s .c o m*/
 * @param context Context.
 */
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private static void calculateDisplaySize(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point screenSize = new Point();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(screenSize);

        displayWidth = screenSize.x;
        displayHeight = screenSize.y;
    } else {
        displayWidth = display.getWidth();
        displayHeight = display.getHeight();
    }
}