With Android's Multi-Window support, how do I detect if the status bar is visible? For instance, when in portrait orientation, the status bar might be visible if I'm the top app, but won't be visible when I'm the bottom app. Right now, my views are funny when on the bottom because I make space for the status bar that isn't there anymore.
Asked
Active
Viewed 1,744 times
16
-
3Don’t make space for the status bar programmatically. Use android:fitsSystemWindows , View.onApplyWindowInsets(), View.setOnApplyWindowInsetsListener(), Behavior.onApplyWindowInsets() ... . It’s hard work, but you can’t rely on the status bar being there, and we knew that before N. I suggest you take time to re-implement everything by using WindowInsets. – natario Sep 29 '16 at 16:31
-
WindowInsets work for providing the information programatically, but they are slow. You get multiple calls before you find out what the real insets are. – Justin Oct 05 '16 at 19:07
-
As for re-implementing, I don't want to. I rely on many views and some libraries. They don't all play nice with fits system windows. Re-implementing and testing on Jelly Bean, KitKat and Marshmallow across all manufacturers is a last resort. – Justin Oct 05 '16 at 19:12
-
Yes, you will receive multiple calls. It's also the only reliable way of obtaining the insets. How do you think widgets from the support library such as NavigationView or DrawerLayout handle it? That's right, window insets listener (or rather its compat counterpart). – Eugen Pechanec Oct 05 '16 at 19:22
-
`I don’t want to` - I didn’t want either. But this way I was able to update to N gracefully. These kind of recommendations are to be followed, or you’ll find your app messed up sooner or later, due to new APIs, new manufacturers, new devices with unpredictable insets. – natario Oct 05 '16 at 19:41
1 Answers
-1
Assuming you mean the system UI bar, ie the status bar do this:
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});
That is directly from the docs: https://developer.android.com/training/system-ui/visibility.html

Serg
- 22,285
- 5
- 21
- 48

Fred Grott
- 3,505
- 1
- 23
- 18
-
Did you test this in multi-window environment? Seems out of context to me. I don't see how this helps OP determining by how much they need to offset content. – Eugen Pechanec Oct 04 '16 at 22:27
-
ahem its right form the Google docs..it has already been tested by in fact Google – Fred Grott Oct 05 '16 at 18:31
-
1The system ui visibility doesn't change when in multi-window mode. I get the same response whether fullscreen or in multi-window mode. – Justin Oct 05 '16 at 19:05
-
@FredGrott Ahem, that post has been there at least since KitKat and the code does exactly what it looks like: report fullscreen layout. It does not provide any information about **system insets** which is what OP is asking for. – Eugen Pechanec Oct 05 '16 at 19:13