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

F958fbf402a67a1742335a4894e041e5?s=128

magiepooh

July 22, 2016
Tweet

Transcript

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

    AdjustFullScreenLayout
  2. About me @magiepooh @magie_pooh Takuma Fujita

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

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

    AdjustFullScreenLayout
  5. 1. Refrection

  6. Reflection <!-- Height of the status bar --> <dimen name="status_bar_height">24dp</dimen>

    <!-- Height of the bottom navigation / system bar. --> <dimen name=“navigation_bar_height">48dp</dimen> <!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height --> <dimen name=“navigation_bar_height_landscape">48dp</dimen> <!-- Width of the navigation bar when it is placed vertically on the screen --> <dimen name="navigation_bar_width">42dp</dimen>
  7. Reflection // portrait resources.getIdentifier("navigation_bar_height", "dimen", “android"); // landscape resources.getIdentifier("navigation_bar_height_l andscape",

    "dimen", "android");
  8. But … • Some device with hardware keyboard return wrong

    size • tablet? mobile??
  9. 2. Calculate

  10. Calculate StatusBar and NavigationBar Size • getSize • getCurrentSizeRange •

    getRealSize(getRawSize)
  11. getRealSize getSize getCurrentSizeRange

  12. getRealSize getSize getCurrentSizeRange

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

    }
  14. NavigationBar Height public int getNavigationBarHeight() {
 return getRealSize().y - getDisplaySize().y;


    }
  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; } }
  16. 3. WindowInsetsCompat

  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; } }
  18. ViewCompat.setOnApplyWindowInsetsListener(view, new ApplyInsetsListener());

  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(); }
  20. WTF!!!! WindowInsetsCompat is very useful class!

  21. Thanks for your attention.