android get screen size of the other screen orientation
Asked Answered
S

3

6

since android 3.0 there is a navigation bar with back and home button on the display. Now if I get the screen size like this

ClockLwp.ScreenHeight = getWindowManager().getDefaultDisplay().getHeight();
ClockLwp.ScreenWidth = getWindowManager().getDefaultDisplay().getWidth();

of this

getResources().getDisplayMetrics().widthPixels
getResources().getDisplayMetrics().heightPixels

I get different values for the landscape and portrait. Here is an example what I mean:

Acer Iconia A500 portrait mode : 800 x 1232 Acer Iconia A500 landscape mode : 1280 x 752

AFAIK this goes for all devices with such a navigation bar. (e.g. Samsung Galaxy Nexus, GalaxyTab 10.1 etc.)

Now my question is, if I am in portrait mode how can I get the values of landscape mode and the other way around?

Sasaki answered 21/2, 2012 at 16:19 Comment(0)
V
1

since android 3 there is no way with the public api to retrive the raw heigth and width . You can go through reflection. Look for getRawHeigth/Width

Vinic answered 21/2, 2012 at 16:51 Comment(4)
It's not really the raw height an width I want. It's the width and height values after subtracting the navigation bar height, but of the other orientation.Sasaki
I don't know how I can get the height of the navigation bar. (containing the back and home button, not sure how this bar is called correctly.)Sasaki
RawHeigth - Display.getHeight() should be give you the bar heigthVinic
does not work. the bar is on some devices on the right of the screen instead of at the bottom it.Assiduity
U
1

EDIT (26 September 2014): I reformulated my text to contain a full answer and not just comment to Dmitri solution.

Here is the part that we are using to calculate the navigation bar height before rotation:

DisplayMetrics metrics = new DisplayMetrics();
mFragment.getActivity().getWindowManager().getDefaultDisplay()
            .getMetrics(metrics);

/* 
 * We need to take system resources here to verify navigation 
 * bar height. Application resources also contains navigation bar 
 * height but this value is not valid for some devices e.g. Nexus 7 (2012)! 
 */
Resources appRes = getResources();
Resources sysRes = Resources.getSystem();
boolean landscapeMode = (metrics.widthPixels > metrics.heightPixels);
/* Check if the device has navigation bar. There is no direct API for 
 * that so we use some hacking that may not always work,
 * (see https://mcmap.net/q/153200/-check-for-navigation-bar)
 * but this method should be enough for most devices.
 * TODO: Find a more consistent way. For our needs it is currently enough as we 
 * support limited set of devices.
 */
boolean hasNavigationBar = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
int navBarHeight = 0;
int navBarHeightRotated = 0;
if (hasNavigationBar) {
    int barHeightDimId = 0;
    int barHeightDimIdRotated = 0;
    if (landscapeMode) {
        barHeightDimId = sysRes.getIdentifier(
                "navigation_bar_height_landscape", "dimen", "android");
        barHeightDimIdRotated = sysRes.getIdentifier(
                "navigation_bar_height", "dimen", "android");
    } else {
        barHeightDimIdRotated = sysRes.getIdentifier(
                "navigation_bar_height_landscape", "dimen", "android");
        barHeightDimId = sysRes.getIdentifier("navigation_bar_height",
                "dimen", "android");
    }
    if (barHeightDimId != 0) {
        navBarHeight = appRes.getDimensionPixelSize(barHeightDimId);
    }
    if (barHeightDimIdRotated != 0) {
        navBarHeightRotated = appRes
                .getDimensionPixelSize(barHeightDimIdRotated);
    }
    NCLogs.i("MyApp", String.format("Android navigation bar height: %d (current), %d (after rotation)", navBarHeight, navBarHeightRotated));
}

then we can calculate our view width and height after rotation:

int realScreenHeight = metrics.heightPixels + navBarHeight;

// We expect to not have decor views near the left or right borders
int realScreenWidth = metrics.widthPixels;

/*
 * Height of decor views: system status bar + any application addtional elements + 
 * + system navigation bar
 */
int decorHeightRotated = realScreenHeight - getHeight() - navBarHeight + navBarHeightRotated; 

int viewWidthRotated = realScreenHeight;
int viewHeightRotated = (realScreenWidth - decorHeightRotated);
NCLogs.i("MyApp", String.format("Width after rotation: %d, Height after rotation: %d", viewWidthRotated, viewHeightRotated));
Unidirectional answered 25/9, 2014 at 9:41 Comment(0)
M
0

Here is solution that works for me. I've got real screen size and then subtract navigation bar height.

    // get navigation bar height for landscape and portrait modes 
    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    int resourceIdLandscape = resources.getIdentifier("navigation_bar_height_landscape", "dimen", "android");
    int portraitNavBarHeight = 0;
    int landscapeNavBarHeight = 0;

    boolean hasPermanentMenuKey = ViewConfiguration.get(activity).hasPermanentMenuKey();
    if (resourceId > 0 && !hasPermanentMenuKey) {
        portraitNavBarHeight = resources.getDimensionPixelSize(resourceId);
    }

    if (resourceIdLandscape > 0 && !hasPermanentMenuKey) {
        landscapeNavBarHeight = resources.getDimensionPixelSize(resourceIdLandscape);
    } 

    // get real screen size 
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
    Display disp = wm.getDefaultDisplay();

    int API_LEVEL =  android.os.Build.VERSION.SDK_INT;
    if (API_LEVEL >= 17) {
        disp.getRealMetrics(displayMetrics);
    } else {
        disp.getMetrics(displayMetrics);
    }

    int width = displayMetrics.widthPixels;
    int height = displayMetrics.heightPixels;

    // size for orientation 
    int portraitHeight;
    int portraitWidth;
    int landscapeHeight;
    int landscapeWidth;

    if(width > height) {
        portraitHeight = width - portraitNavBarHeight;
        portraitWidth = height;

        landscapeHeight = height - landscapeNavBarHeight;
        landscapeNavBarHeight = width;

    } else {
        portraitHeight = height - portraitNavBarHeight;
        portraitWidth = width;

        landscapeHeight = width - landscapeNavBarHeight;
        landscapeNavBarHeight = height;
    }

If your application has status bar

public static int getStatusBarHeight(Context context) {
    Resources resources = context.getResources();
    int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    }
    return 0;
} 
Muffin answered 14/3, 2014 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.