Search code examples
javaandroidandroid-layoutandroid-developer-api

How to Detect Presence and Size of Android Taskbar


My app recognizes the presence of the navigation bar at the bottom of the screen and moves its own buttons up and out of the way so as not to be obscured. It also recognizes when "swipe gestures" have been turned on and reduces the amount of space reserved at the bottom of the screen.

My problem is that some devices have a "taskbar" that can appear across the bottom that is independent of the navigation bar, though it will share that bar if both are enabled at the same time. I can't figure out how to detect this taskbar and determine its height so I can adjust my layout to avoid it. Any clues?


Solution

  • For some reason I wasn't able to use setOnApplyWindowInsetsListener, perhaps because I haven't converted to AndroidX? I don't remember. I just know it didn't work. Here's what does work:

    public int getNavBarHeight()
        {
        Resources resources = getResources();
        int navigation_bar_height = 0;
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if ( resourceId > 0 )
            {
            navigation_bar_height = resources.getDimensionPixelSize(resourceId);
            }
        return navigation_bar_height;
        }
    

    Then I make adjustments accordingly.