Example usage for android.content Context WINDOW_SERVICE

List of usage examples for android.content Context WINDOW_SERVICE

Introduction

In this page you can find the example usage for android.content Context WINDOW_SERVICE.

Prototype

String WINDOW_SERVICE

To view the source code for android.content Context WINDOW_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.view.WindowManager for accessing the system's window manager.

Usage

From source file:Main.java

public static int getDisplayWidthPixel(@NonNull Context context) {
    return getDisplayWidthPixel(
            ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay());
}

From source file:Main.java

/**
 * Get the screen height.//  ww  w  .j av  a  2  s . co m
 *
 * @param context
 * @return the screen height
 */
public static int getScreenHeight(Context context) {
    if (context != null) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        return displayMetrics.heightPixels;
    } else {
        return 1920;
    }
}

From source file:Main.java

private static void getDisplay(Context context) {
    if (width <= 0 || height <= 0) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;//  w ww.  ja va 2s  . c o m
        height = dm.heightPixels;
    }
}

From source file:Main.java

/**
 * Getter for the screen width in pixels.
 *
 * @param context a reference to the context.
 * @return the screen width in pixels./*w w  w  .jav a 2 s .  co  m*/
 */
public static int getScreenWidth(Context context) {
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size.x;
}

From source file:Main.java

public static int[] getScreenSize(Context context, boolean useDeviceSize) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();//from  www. ja v  a  2 s.  c o  m
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    if (!useDeviceSize) {
        size[0] = widthPixels;
        size[1] = heightPixels;
        return size;
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
        try {
            widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
            heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
        } catch (Exception ignored) {
        }
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17)
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
            widthPixels = realSize.x;
            heightPixels = realSize.y;
        } catch (Exception ignored) {
        }
    size[0] = widthPixels;
    size[1] = heightPixels;
    return size;
}

From source file:Main.java

public static boolean shouldScaleDownBitmap(Context context, Bitmap bitmap) {
    if (context != null && bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);/*from w w  w. j a  v a  2 s . c  o  m*/
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        return ((width != 0 && width / bitmap.getWidth() < 1)
                || (height != 0 && height / bitmap.getHeight() < 1));
    }
    return false;
}

From source file:Main.java

public static int[] getScreenSize(Context context, boolean useDeviceSize) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();/*from  w w w  .j  av a 2  s .  c om*/
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    if (!useDeviceSize) {
        size[0] = widthPixels;
        size[1] = heightPixels - getStatusBarHeight(context);

        return size;
    }

    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
        try {
            widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
            heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
        } catch (Exception ignored) {
        }
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17)
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
            widthPixels = realSize.x;
            heightPixels = realSize.y;
        } catch (Exception ignored) {
        }
    size[0] = widthPixels;
    size[1] = heightPixels;
    return size;
}

From source file:Main.java

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public static void resizeImageView(Context ctx, ImageView imgView) {
    WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int width;/*from  w  w w  .j a  v  a 2 s. co  m*/
    // int height;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
        width = display.getWidth(); // deprecated
        // height = display.getHeight(); // deprecated
    } else {
        Point size = new Point();
        display.getSize(size);
        width = size.x;
        // height = size.y;
    }

    imgView.setMinimumHeight(width);
    imgView.setMinimumWidth(width);
    imgView.getLayoutParams().height = width;
    imgView.getLayoutParams().width = width;
}

From source file:Main.java

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static int[] getScreenWidthHeight(Context context) {

    int[] widthAndHeight = new int[2];

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

    if (android.os.Build.VERSION.SDK_INT >= 13) {
        Point size = new Point();
        display.getSize(size);/*from w  w w.  java2 s  .c o m*/
        widthAndHeight[0] = size.x;
        widthAndHeight[1] = size.y;
    } else {
        widthAndHeight[0] = display.getWidth();
        widthAndHeight[1] = display.getHeight();
    }

    return widthAndHeight;
}

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 {/*ww w  .j  av a2 s .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;
}