0

Some Android applications still use the menu button functionality that was deprecated since 3.0. Many new phones (Nexus for instance) have software navigation buttons and this menu icon is appended to the navigation bar when an application uses it and has not defined an ActionBar.

To get usable screen dimensions in this scenario, I can subtract the notification bar from the total screen height:

DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
int height = dm.heightPixels - getStatusBarHeight(mContext);

where getStatusBarHeight is a function defined elsewhere.

However, if the phone has hardware navigation buttons, an extra navigation row is displayed by the application at the bottom, as shown in the attached picture.

Bottom row with navigation actions appended to application

In this case the previous solution does not work, since the added navigation bar needs to be subtracted.

I'm looking to write a general function that solves for both cases, returning the usable screen dimensions/coordinates by subtracting the software navigation bar and the notification bar. I want to avoid vendor/device specific checks.

There seems to be a way of solving this using getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle); but which values are needed from the Window?

liarspocker
  • 2,434
  • 3
  • 17
  • 26
  • I think this still counts as hardware button and this should work: http://stackoverflow.com/questions/9044907/android-programatically-detect-if-device-has-hardware-menu-button – ntv1000 Jul 08 '14 at 13:39
  • Hi @ntv1000. `ViewConfiguration.get(context).hasPermanentMenuKey()` returns true when there is a hardware menu button, which in this case is false. Android will draw the additional row, so they're different cases – liarspocker Jul 08 '14 at 13:44

1 Answers1

0

To get the screen dimensions, I simply use:

final DisplayMetrics DISPLAY_METRICS = ((Context)this).getResources().getDisplayMetrics();

Then retrieve the dimensions using:

final int DISPLAY_HEIGHT = DISPLAY_METRICS.heightPixels;
final int DISPLAY_WIDTH = DISPLAY_METRICS.widthPixels;

Or when I only need one dimension:

final int DISPLAY_HEIGHT = this.getResources().getDisplayMetrics().heightPixels;
Nic
  • 6,211
  • 10
  • 46
  • 69
  • Just to clarify: To get just the width, you would use `this.getResources().getDisplayMetric().widthPixels`? – Nic May 21 '15 at 22:05
  • @QPaysTaxes "getDisplayMetric" needs an s -> "getDisplayMetrics" Such as "getResources().getDisplayMetrics().widthPixels();" works if you are in an activity. – Kevin Kozakewich Jun 02 '15 at 13:01