Home » Android » android – Briefly hiding ActionBar without resizing Activity

android – Briefly hiding ActionBar without resizing Activity

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m using a ViewPager to scroll between different fragments. There are two types of fragments, using two different menu resources. I’m invalidating the menu to switch between those resources when necessary. That’s all working pretty well, but the menu is “redrawn” without an animation.

To prevent having to mess with individual MenuItems I was hoping I could briefly hide the ActionBar while the new menu is loaded, showing it when that’s done. That’s working as expected as well, but the activity is resized when the ActionBar is toggled.

Is there any way to prevent this from happening, or otherwise hide the ugly transition between menu resources?

How to&Answers:

I didn’t quite catch the menu part of your problem, but there is an easy solution to preventing your activity from resizing when the ActionBar appears or disappears.

You can tell the ActionBar to draw itself in overlay mode, meaning it will float on top of the activity, in stead of actually being part of the activity’s layout. Use either android:windowActionBarOverlay in your theme, or the Window.FEATURE_ACTION_BAR_OVERLAY flag from code.

You probably want to use this feature in conjunction with the actionBarSize constant, that specifies the correct offset for the first view in your layout. This way, your content still appears below the ActionBar, but since the ActionBar itself is an overlay, upon hiding/showing it, the activity will not resize.

<SomeView
    ...
    android:layout_marginTop="?android:attr/actionBarSize" />

More details can be found in the documentation.