Example usage for android.app Activity getWindowManager

List of usage examples for android.app Activity getWindowManager

Introduction

In this page you can find the example usage for android.app Activity getWindowManager.

Prototype

public WindowManager getWindowManager() 

Source Link

Document

Retrieve the window manager for showing custom windows.

Usage

From source file:Main.java

public static int getScreenWidth(Activity context) {
    if (screenWidth == 0 || screenHeight == 0) {
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenDensity = dm.density;//w ww. j  ava 2  s .  c  o m
        screenHeight = dm.heightPixels;
        screenWidth = dm.widthPixels;
    }
    return screenWidth;
}

From source file:Main.java

public static int getScreenHeight(Activity context) {
    if (screenWidth == 0 || screenHeight == 0) {
        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenDensity = dm.density;// w  ww.j a v a  2  s.  com
        screenHeight = dm.heightPixels;
        screenWidth = dm.widthPixels;
    }
    return screenHeight;
}

From source file:Main.java

/**
 * Gets the screen size in pixels in a backwards compatible way
 *
 * @param caller        Activity calling; needed to get access to the {@link android.view.WindowManager}
 * @return              Size in pixels of the screen, or default {@link Point} if caller is null
 *///ww  w  . j  av a 2  s  . c o  m
public static Point getScreenSize(Activity caller) {
    Point size = new Point();
    if (caller != null) {
        Display display = caller.getWindowManager().getDefaultDisplay();
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
            display.getSize(size);
        } else {
            size.set(display.getWidth(), display.getHeight());
        }
    }
    return size;
}

From source file:Main.java

public static int getScreenOrientation(Activity activity) {
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width
            || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) {
        switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        case Surface.ROTATION_90:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_180:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        case Surface.ROTATION_270:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        default:/*ww  w  .  ja  v  a 2 s. c  o  m*/
            Log.e("getScreenOrientation", "Unknown screen orientation. Defaulting to portrait.");
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        }
    } else {
        // if the device's natural orientation is landscape or if the device
        // is square:

        switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_90:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        case Surface.ROTATION_180:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        case Surface.ROTATION_270:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        default:
            Log.e("getScreenOrientation", "Unknown screen orientation. Defaulting to landscape.");
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        }
    }

    return orientation;
}

From source file:Main.java

/**
 * Gets the screen density for the device.
 * /*from w  w w  .  ja v  a  2 s .co  m*/
 * @param activity
 *          is the current activity.
 * 
 * @return A double value representing the device's screen density.
 */
public static double getDensity(Activity activity) {
    if (density == -1) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        density = displayMetrics.density;
    }

    return density;
}

From source file:Main.java

public static int[] getScreenHeightAndWidth(Activity activity) {
    int[] result = new int[2];

    try {//from w  w w. j ava  2 s  .  c o m
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        result[0] = displayMetrics.widthPixels;
        result[1] = displayMetrics.heightPixels;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * set the camera display orientation based on the activity's rotation
 * /*from  www  .  jav  a2  s.co  m*/
 * @param activity
 * @param cameraId
 * @param camera
 */
@SuppressLint("NewApi")
public static void setCameraDisplayOrientation(Activity activity, int cameraId,
        android.hardware.Camera camera) {
    final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    final int rotDeg = getCameraRotationForSurfaceRotation(rotation, camera, cameraId);
    if (rotDeg != 0) {
        camera.setDisplayOrientation(rotDeg);
    }
}

From source file:Main.java

/**
 * Reports to the log information about the device's display. This information includes
 * the width and height, and density (low, medium, high). 
 * @param activity The Activity to report on.
 *//*from www  . j  ava2 s  .c o  m*/
public static void reportDisplayInformation(Activity activity) {

    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int displayWidth = metrics.widthPixels;
    int displayHeight = metrics.heightPixels;

    String density = "unknown";
    switch (metrics.densityDpi) {
    case DisplayMetrics.DENSITY_LOW:
        density = "Low";
        break;
    case DisplayMetrics.DENSITY_MEDIUM:
        density = "Medium";
        break;
    case DisplayMetrics.DENSITY_HIGH:
        density = "High";
        break;
    }

    Log.i(TAG, "Display is " + displayWidth + "x" + displayHeight + ", Density: " + density);

}

From source file:Main.java

/**
 * Untested, taken from stack-overflow./*  w w w . j  a  va2  s.  co m*/
 *
 * @param activity
 */
public static void disableScreenOrientationChange(Activity activity) {
    final int orientation = activity.getResources().getConfiguration().orientation;
    final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();

    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    } else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
        if (orientation == Configuration.ORIENTATION_PORTRAIT) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
        } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
    }
}

From source file:Main.java

public static int getImageButtonSize(Activity activity) {
    if (mImageButtonSize == -1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            mImageButtonSize = 16;/*from   ww w .  j  ava 2 s.c  o  m*/
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            mImageButtonSize = 32;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mImageButtonSize = 48;
            break;
        default:
            mImageButtonSize = 32;
        }
    }

    return mImageButtonSize;
}