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 full-size slide

  2. About me
    @magiepooh
    @magie_pooh
    Takuma Fujita

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. 1. Refrection

    View full-size slide

  6. Reflection

    24dp

    48dp

    48dp

    42dp

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. 2. Calculate

    View full-size slide

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

    View full-size slide

  11. getRealSize
    getSize
    getCurrentSizeRange

    View full-size slide

  12. getRealSize
    getSize
    getCurrentSizeRange

    View full-size slide

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

    View full-size slide

  14. NavigationBar Height
    public int getNavigationBarHeight() {

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

    }

    View full-size 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 full-size slide

  16. 3. WindowInsetsCompat

    View full-size 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 full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

  21. Thanks for your attention.

    View full-size slide