Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to get size of NavigationBar and StatusBar

How to get size of NavigationBar and StatusBar

How to get size of NavigationBar and StatusBar for Android

magiepooh

July 22, 2016
Tweet

More Decks by magiepooh

Other Decks in Technology

Transcript

  1. How to get size of
    NavigationBar and
    StatusBar
    @magiepooh
    https://github.com/magiepooh/
    AdjustFullScreenLayout

    View Slide

  2. About me
    @magiepooh
    @magie_pooh
    Takuma Fujita

    View Slide

  3. Company
    AbemaTV, Inc.
    (Cyber Agent, Inc.)

    View Slide

  4. How to get size of
    NavigationBar and
    StatusBar
    @magiepooh
    https://github.com/magiepooh/
    AdjustFullScreenLayout

    View Slide

  5. 1. Refrection

    View Slide

  6. Reflection

    24dp

    48dp

    48dp

    42dp

    View Slide

  7. Reflection
    // portrait
    resources.getIdentifier("navigation_bar_height",
    "dimen", “android");
    // landscape
    resources.getIdentifier("navigation_bar_height_l
    andscape", "dimen", "android");

    View Slide

  8. But …
    • Some device with hardware keyboard return
    wrong size
    • tablet? mobile??

    View Slide

  9. 2. Calculate

    View Slide

  10. Calculate StatusBar and
    NavigationBar Size
    • getSize
    • getCurrentSizeRange
    • getRealSize(getRawSize)

    View Slide

  11. getRealSize
    getSize
    getCurrentSizeRange

    View Slide

  12. getRealSize
    getSize
    getCurrentSizeRange

    View Slide

  13. NavigationBar Width
    public int getNavigationBarWidth() {
    return getRealSize().x - getDisplaySize().x;
    }

    View Slide

  14. NavigationBar Height
    public int getNavigationBarHeight() {

    return getRealSize().y - getDisplaySize().y;

    }

    View Slide

  15. StatusBar Height
    public int getStatusBarHeight() {
    Point smallSize = new Point();
    Point largeSize = new Point();
    display.getCurrentSizeRange(smallSize, largeSize);
    // getDisplaySize() = getSize()
    Point displaySize = getDisplaySize();
    if (displaySize.x > displaySize.y) {
    return displaySize.y - smallSize.y;
    } else {
    return displaySize.y - largeSize.y;
    }
    }

    View Slide

  16. 3. WindowInsetsCompat

    View Slide

  17. private class ApplyInsetsListener implements
    android.support.v4.view.OnApplyWindowInsetsListener {
    @Override
    public WindowInsetsCompat onApplyWindowInsets(View v,
    WindowInsetsCompat insets) {
    if (mLastInsets != insets) {
    mLastInsets = insets;
    }
    return insets;
    }
    }

    View Slide

  18. ViewCompat.setOnApplyWindowInsetsListener(view,
    new ApplyInsetsListener());

    View Slide

  19. public int getNavigationBarWidth() {
    return mLastInsets == null ? 0 :
    mLastInsets.getSystemWindowInsetRight();
    }
    public int getNavigationBarHeight() {
    return mLastInsets == null ? 0 :
    mLastInsets.getSystemWindowInsetBottom();
    }
    public int getStatusBarHeight() {
    return mLastInsets == null ? 0 :
    mLastInsets.getSystemWindowInsetTop();
    }

    View Slide

  20. WTF!!!!
    WindowInsetsCompat is
    very useful class!

    View Slide

  21. Thanks for your attention.

    View Slide