I have an app for Android 4.0 that runs in landscape mode only. In one activity, I want to present different UIs depending on how much height is available. I've set up the appropriate layout qualifier directories and they are being selected, but not in the expected way.
My folders are:
- res/layout
- res/layout-h360dp
So far, so good. Here's where it starts to go off the rails. The app is running in full screen mode, the only chrome being the bar with the back, home, and switch app buttons, which in any case is on the side and does not affect available height.
The app reports having 384dp of height available. I even put a vertical bar on the screen that is 383dp tall and it correctly shows with a small sliver of space below it. There is clearly 384dp available.
But the app will not use the file in res/layout-h360dp! I tried different numbers until it would work, and I found that the cutoff is 360dp. When I change the name to layout-h359dp, it works, so the app thinks it only has 359dp available, but clearly it has more, as evidenced by the ability to draw the vertical bar with room to spare. Is this a bug in the OS?
Here's the code I'm using to double-check available space programmatically.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
// Reports 1196x768
int width = size.x;
int height = size.y;
// Reports 598x384
float widthDp = MeasuringUtilities.convertPixelsToDp(width, this);
float heightDp = MeasuringUtilities.convertPixelsToDp(height, this);
// WTF? Reports 598x359! Why?!
float resourcesWidth = this.getResources().getConfiguration().screenWidthDp;
float resourcesHeight = this.getResources().getConfiguration().screenHeightDp;
The conversion of pixels to dp is via this routine:
public static float convertPixelsToDp(float px, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
I take some tiny amount of solace in the fact that at least getConfiguration().screenHeightDp reports a number that appears to be being used. But why? The actual visible, usable height is 384. Why report 359?
Please help! - Sleepless in Seattle