Search code examples
androidfullscreentoolbar

Android closing full screen view at the bottom is shunted off the screen


I have a screen with a bottom toolbar aligned using the following styling

<style name="BottomToolbar" >
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@color/WHITE</item>
    <item name="android:layout_gravity">bottom</item>
    <item name="android:layout_alignParentBottom">true</item>
    <item name="android:minHeight">?attr/actionBarSize</item>
</style>

When I first enter my screen the toolbar is nicely aligned to the bottom of the screen.

However, If I go to full screen mode (which hides the toolbar) and then return to normal mode (which re-displays the toolbar) the toolbar is now placed slightly off the screen. It looks as if its shifted down the amount of the status bar that is hidden in full screen mode.

enter image description here

If I background my app and then foreground it, the bottom toolbar is rendered correctly.

I have a bar at the top of the screen (instead of an appBar) and that is rendered in the correct place. I can not work out why it is misplacing the bottom toolbar.

Its as if when it is determining the bottom of the parent it is not adjusting for the fact its not full screen height anymore.

What I have tried

I have tried setting "fitsSystemWindows="true"" on my toolbar when exiting full screen mode but that doesn't make a difference.

I have tried setting the toolbar to INVISIBLE rather than GONE when hiding it but that makes no difference.

EDIT 1 - Added Code snippet of how I go into and out of full screen

I go into full screen using the following

getActivity().getWindow().getDecorView().setSystemUiVisibility(
      View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    | View.SYSTEM_UI_FLAG_FULLSCREEN
    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

I exit full screen by using the following code

getActivity().getWindow().getDecorView().setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

EDIT 2 - View is not shoved off the bottom, its height is too small

It appears that the bottom view is not shoved off the bottom of the screen, its height is shrunk and smaller than it should be.

The bottom view is a LinearLayout with its height set to "wrap_content" but it appears the height is not calculated correctly as its shorter than its contents and hence the bottom of its contents are cut off.

Edit 3 - devices the issues presents on

Further investigation showed the issue only presents on devices which do not have the on screen navigation bar. For example the issue is on my Samsung 10" Tablet but not on my Nexus 5x.


Solution

  • If I remove the following flag the issue appears to be fixed

    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    

    The Google doc for this flag is "When using other layout flags, we would like a stable view of the content insets given to fitSystemWindows(Rect)." https://developer.android.com/reference/android/view/View.html

    The docs say "You may also need to use SYSTEM_UI_FLAG_LAYOUT_STABLE to help your app maintain a stable layout." (https://developer.android.com/training/system-ui/status.html) and "It's good practice to include other system UI flags (such as SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION and SYSTEM_UI_FLAG_LAYOUT_STABLE) to keep the content from resizing when the system bars hide and show." (https://developer.android.com/training/system-ui/immersive.html)