Search code examples
androidandroid-actionbar

How to dynamic show(hide) action bar


My application contains a single AppCompatActivity and some Fragments, in some fragments, I want to hide the action bar (for example, the login fragment), in other fragments, I want to show the action bar. The application has a theme <style name="AppTheme" parent="Theme.MaterialComponents.Light">, this theme has an action bar, I want to know how to show or hide the action bar dynamically in code.

I have tried getSupportActionBar().hide(); but this only made the action bar translucence,it's still hold the space.


Solution

  • I would suggest using a Theme with no ActionBar and adding your own Toolbar into your activity_layout.xml. Once you have it in your layout you can easily hide it with toolbar.setVisibility(View.GONE);. If you'd like to animate hiding and showing it, you can do so with TransitionManager.beginDelayedTransition(). Depending on the effect you'd like to achieve, you can also try setting a translationY to hide the toolbar from the screen.