Mark Dappollone
Mark Dappollone

Reputation: 1530

Android : Fullscreen system visibility screws up window insets

I'm writing an app that has one activity and a bunch of fragments and one of those fragments is fullscreen. In order to achieve that, I'm trying to use the System UI Visibility flags.

For the fullscreen fragment, I set the visibility:

layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

As soon as I do that, the window insets change to values that don't seem to make any sense. Then, when I exit that fullscreen fragment, I wind up with wrong padding on my view (based on the weird insets)

enter image description here

Does anyone know what's going on here? The way I understand the window insets must be wrong, because this behavior seems completely counter intuitive. Any insight would be appreciated.

To view the full source of the test app I wrote to illustrate the problem go here:

https://github.com/dapp/visibilitytest

Upvotes: 5

Views: 2687

Answers (2)

Hexise
Hexise

Reputation: 1568

I have the same problem today. I am using a NavigationView from design library. When I exit full screen, there are insects on top and bottom of NavigationView, which is ugly.

I debug and dig into the source code of NavigationView and find following solution:

  1. Please use setFitsSystemWindows(false) method for your view, in my case, for NavigationView, to avoid impact of status bar and navigation bar changes.

  2. I found there is OnApplyWindowInsetsListener in NavigationView's super class: ScrimInsetsFrameLayout, then I clear this listener to avoid impact when full screen is enabled/disabled, by using ViewCompat.setOnApplyWindowInsetsListener(mNavigationView, null).

Now the NavigationView behaves correctly when exit full screen, hope this helps for your problem.

Upvotes: 1

ianhanniballake
ianhanniballake

Reputation: 199880

setSystemUiVisibility() propagates the system UI visibility changes up the view hierarchy (to request new insets).

However, when you call layout.setSystemUiVisibility() in your Fragment's onCreateView(), the layout is not yet attached to the view hierarchy. This is what causes your issues with the insets not being updated appropriately.

You can instead call getActivity().getWindow().getDecorView().setSystemUiVisibility() in onCreateView() and you'll find that all insets work correctly.

Upvotes: 0

Related Questions