How To Show and hide ActionBar with AppCompat v.7
Solution 1:
In the activities where you want to have no action bar use a theme derived from Theme.AppCompat.NoActionBar
or Theme.AppCompat.Light.NoActionBar
. This activity will never be able to show the action bar unless you supply your own via setSupportActionBar(Toolbar)
.
In the activities where you want to have the action bar use a theme derived from Theme.AppCompat
, Theme.AppCompat.Light
or Theme.AppCompat.Light.DarkActionBar
. This will allow you to dynamically hide or show the action bar in such activity. You will not be able to supply your own action bar using these themes.
When working with appcompat-v7 action bar you need to obtain it by calling getSupportActionBar()
instead of getActionBar()
.
Solution 2:
You can hide and show the ActionBar programatically.
To hide
getSupportActionBar().hide();
To Show
getSupportActionBar().show();
It can also be done from Fragments
To hide
getActivity().getSupportActionBar().hide();
To Show
getActivity().getSupportActionBar().show();
Edit:
As noted by @RenniePet,
You need to call ((AppCompatActivity)getActivity()).getSupportActionBar()
, if you're using Toolbar
as the ActionBar
.
Solution 3:
Extend you activity as AppCompatActivity
and then use action bar as:-
getSupportActionBar().hide(); // for hiding
getSupportActionBar().show(); // for showing
Solution 4:
<style name="Theme.AppCompat.NoActionBar" parent="Theme.AppCompat.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
remove android in item windowActionbar.just like follow:
<style name="Theme.AppCompat.NoActionBar" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
I hope it can help you.