1

How can I reliably measure the height of the soft navigation on Android so that it doesn't interfere with our app icons, as per the image below?

enter image description here

I've tried several other answers from here but they aren't reliable -some devices will report a height even if the keys are hardware-based, methods of detecting the bottom inset aren't reliable and methods of detecting whether or not the nav is software or hardware isn't reliable either.

I'm sure there must be some kind of Android standard for handling this situation?

Le-roy Staines
  • 1,909
  • 1
  • 19
  • 38

1 Answers1

0

The "Android standard" is generally for your app to not overlap the navigation bar, hence avoiding this issue. E.g. adding android:fitsSystemWindows="true" to your root layout.

Otherwise, setting the colour is usually enough. If you absolutely have to overlap it, I used this a few years back and it worked across our test devices:

  fun navigationBarHeight(context: Context): Int {
    val resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android")
    return if (resourceId > 0) {
      context.getResources().getDimensionPixelSize(resourceId)
    } else 0
  }

This question suggests that approach may no longer be 100% accurate though. Easiest is to avoid the issue altogether!

Jake Lee
  • 5,837
  • 7
  • 39
  • 73
  • Yeah unfortunately that returns a height even on some devices that have hardware keys (My Huawei Mate 9 pro for example). It just looks nicer overlapped! Setting the colour might be ok though – Le-roy Staines Apr 05 '21 at 21:54
  • Setting the colour is much more reliable, and avoid this headache :) – Jake Lee Apr 05 '21 at 22:11