How to get size of NavigationBar and StatusBar for Android
How to get size ofNavigationBar andStatusBar@magiepoohhttps://github.com/magiepooh/AdjustFullScreenLayout
View Slide
About me@magiepooh@magie_poohTakuma Fujita
CompanyAbemaTV, Inc.(Cyber Agent, Inc.)
1. Refrection
Reflection24dp48dp48dp42dp
Reflection// portraitresources.getIdentifier("navigation_bar_height","dimen", “android");// landscaperesources.getIdentifier("navigation_bar_height_landscape", "dimen", "android");
But …• Some device with hardware keyboard returnwrong size• tablet? mobile??
2. Calculate
Calculate StatusBar andNavigationBar Size• getSize• getCurrentSizeRange• getRealSize(getRawSize)
getRealSizegetSizegetCurrentSizeRange
NavigationBar Widthpublic int getNavigationBarWidth() {return getRealSize().x - getDisplaySize().x;}
NavigationBar Heightpublic int getNavigationBarHeight() { return getRealSize().y - getDisplaySize().y; }
StatusBar Heightpublic 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;}}
3. WindowInsetsCompat
private class ApplyInsetsListener implementsandroid.support.v4.view.OnApplyWindowInsetsListener {@Overridepublic WindowInsetsCompat onApplyWindowInsets(View v,WindowInsetsCompat insets) {if (mLastInsets != insets) {mLastInsets = insets;}return insets;}}
ViewCompat.setOnApplyWindowInsetsListener(view,new ApplyInsetsListener());
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();}
WTF!!!!WindowInsetsCompat isvery useful class!
Thanks for your attention.