15

On some devices such as Samsung S8, navigation bar can be hide or show, that's a question in some condition. Samsung S8's navigation bar can be hide or show by click left bottom button

I didn't find straight way to determine even if in the Android sources code.

And I google some issues, such as A good solution to check for navigation bar , but it doesn't help.

Any help is very appreciated.

Smiles
  • 461
  • 6
  • 15
  • Do you find any answer ? I am stuck with same problem – TUSHAR Sep 25 '17 at 10:56
  • @TUSHAR No, I still cannot find a good solution. I think Android should make it standardized. – Smiles Sep 27 '17 at 02:54
  • 3
    With more and more full screen phones appears, not just Samsung Galaxy S8/Note8, but Xiaomi, Huawei, etc. If The Android Sources don't make it standardized ASAP, this can become a serious problem. – Smiles Nov 13 '17 at 07:32

2 Answers2

5

First, credit the original author: https://www.jianshu.com/p/ddfbabd614b6

For the Samsung phones (i.e, S8, S9, etc) you can detect if the navigation bar is showing via listening to a Samsung event.

private static final String SAMSUNG_NAVIGATION_EVENT = "navigationbar_hide_bar_enabled";

Then just listen to this event, and do your thing:

private void checkNavigationBar() {
    if (isSamsungVersionNougat()) { // check if Samsung phones
        // detect navigation bar
        try {
            // there are navigation bar
            if (Settings.Global.getInt(activity.getContentResolver(), SAMSUNG_NAVIGATION_EVENT) == 0) {
                // Your code
                // example: galleryViewModel.navigationBarHeight.set(getNavigationBarHeight());
            } else { // there are no navigation bar
                // Your code
                // example: galleryViewModel.navigationBarHeight.set(0);
            }
        } catch (Exception ignored) {
        }
        barHideEnableObserver = new BarHideEnableObserver(new Handler());
        activity.getContentResolver().registerContentObserver(
                Settings.Global.getUriFor(SAMSUNG_NAVIGATION_EVENT),
                true, barHideEnableObserver);
    } else {
        galleryViewModel.navigationBarHeight.set(getNavigationBarHeight());
    }
}
Haomin
  • 479
  • 5
  • 9
  • Worked for me, this was the only solution we had for finding full screen window height on on Samsung phones that had the collapsible bar – hendersawn Apr 04 '19 at 22:35
  • any idea about how to detect this on devices from other manufacturer? – John Jul 15 '19 at 07:46
  • for xamarin developers with same issue [check this](https://forums.xamarin.com/discussion/156233/how-to-detect-navigation-bar-enable-are-disable-in-samsung-tab-s4) – John Jul 16 '19 at 07:24
  • 1
    okay, this is where it is came from: https://www.jianshu.com/p/ddfbabd614b6 – vigilancer Sep 19 '19 at 19:28
0

Use this method, worked for me. Make sure the view has been rendered first to make sure that getHeight() doesn't return 0. Also make sure that the rootView you are using is meant to take up the whole screen.

public static boolean hasNavBar (Activity activity, View rootView) {
    if (activity == null || rootView == null)
        return true;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
        return true;

    Display d = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics realDisplayMetrics = new DisplayMetrics();
    d.getRealMetrics(realDisplayMetrics);

    int viewHeight = rootView.getHeight();
    if (viewHeight == 0)
        return true;

    int realHeight = realDisplayMetrics.heightPixels;
    return realHeight != viewHeight;
}
  • 1
    It does not work when this flag is set on activity's window: WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS – deadmoto Apr 07 '18 at 18:15