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 getWindowHeight(Context context) {
    if (screenSize == null) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        screenSize = new Point();
        display.getSize(screenSize);/*from   w  w w.j  a v  a  2 s. c  o m*/
    }
    if (screenSize != null)
        return screenSize.y;
    else
        return 0;
}

From source file:Main.java

private static void getScreenMetric(Context cxt) {
    WindowManager wm = (WindowManager) cxt.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);//from  ww  w .  j a v  a 2 s  . c o m
    mScreenWidth = size.x;
    mScreenHeight = size.y;
}

From source file:Main.java

private static int getScreenWidth(@NonNull Context context) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.widthPixels;
}

From source file:Main.java

/**
 * Gets screen width./*from  ww w  . j  a v a 2 s .c o m*/
 *
 * @param context the context
 * @return the screen width in pixels
 */
public static int getScreenWidth(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size.x;
}

From source file:Main.java

public static Point getScreenResolution(Context context) {
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = manager.getDefaultDisplay();
    Point wh = new Point(display.getWidth(), display.getHeight());
    if (wh.x == 0 || wh.y == 0) {
        Log.e("AndroidUtils", "Screen resolution " + wh + " for " + context);
    }//from ww w.j a v  a  2 s. c o m
    return wh;
}

From source file:Main.java

public static void lockOrientation(Activity activity) {
    Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();
    int tempOrientation = activity.getResources().getConfiguration().orientation;
    int orientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
    switch (tempOrientation) {
    case Configuration.ORIENTATION_LANDSCAPE:
        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        else/*from w w w .j a  va  2  s.  c om*/
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        else
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
    }

    activity.setRequestedOrientation(orientation);
}

From source file:Main.java

public static int[] getScreenSize(Context context) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();//from www .  j  a va2 s .c  om
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;
    // 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

/**
 * Get device's resolution size//from w  ww .  j a  v a 2 s.c om
 * @param context {@link Context}
 * @return {@link Display} : device resolution size as display
 */
public static Display getResolutionSize(Context context) throws Exception {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    return display;
}

From source file:Main.java

/**
 * Returns the screen/display size/*w  w w.j a va2s.  co  m*/
 *
 * @param context
 * @return
 */
@SuppressWarnings("deprecation")
public static Point getDisplaySize(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    return new Point(width, height);
}

From source file:Main.java

public static int getDisplayWidth(Context context) {
    if (displayWidth <= 0) {
        WindowManager wm = (WindowManager) context.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        displayWidth = dm.widthPixels;/*  w  w w  .  ja va 2s . c o  m*/
    }
    return displayWidth;
}