Android Multi-Window Support : Detecting if status bar is visible?
Asked Answered
X

1

16

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.

Xerosere answered 22/8, 2016 at 21:42 Comment(5)
Don’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.Aliunde
WindowInsets work for providing the information programatically, but they are slow. You get multiple calls before you find out what the real insets are.Xerosere
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.Xerosere
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).Ruelle
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.Aliunde
L
-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

Littman answered 1/10, 2016 at 15:53 Comment(4)
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.Ruelle
ahem its right form the Google docs..it has already been tested by in fact GoogleLittman
The system ui visibility doesn't change when in multi-window mode. I get the same response whether fullscreen or in multi-window mode.Xerosere
@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.Ruelle

© 2022 - 2024 — McMap. All rights reserved.