Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Show and hide ActionBar with AppCompat v.7

I have a simple app that displays text.

The app starts with a main screen with a few options (ex. an info button that leads to info about the app, a browse button that allows the user to see all the individual pieces of text that can be displayed). The main button leads to another screen where text is displayed. By swiping left and right he can see different passages of text. This is the main purpose of the app.

Currently I have an ActionBar implemented. My MainActivity.Java extends AppCompatActivity. Everything in the app is in this activity.

Now I want to make it so the ActionBar appears only in "Display" mode, not in the start up screen, or "info" / "browse" mode.

Is it possible to have an ActionBar in one part of the app, and no ActionBar in another part of the app? (And keep it all in the same activity?)

I've been trying to accomplish this without avail. If this is possible, what should I try next?

So far, I've attempted the following:

1) Create this theme

<style name="Theme.AppCompat.NoActionBar" parent="Theme.AppCompat.Light">     <item name="android:windowActionBar">false</item>       <item name="android:windowNoTitle">true</item> </style>   

And apply it to MainActivity ...

<activity         android:name=".MainActivity"         android:label="@string/app_name"         android:theme="@style/Theme.AppCompat.NoActionBar" >  

.... After doing this, the ActionBar was still there. (This comes from this S.O. post (android:windowNoTitle will not hide actionbar with appcompat-v7 21.0.0)

2) Another attempt was to add this to onCreate.

public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     getWindow().requestFeature(Window.FEATURE_ACTION_BAR);     getActionBar().hide();      setContentView(R.layout.activity_main); 

This came from studying this S.O. post: (How to hide action bar before activity is created, and then show it again?)

Any suggestions?

like image 818
Timothy Steele Avatar asked May 17 '15 07:05

Timothy Steele


People also ask

Which method is used to hide the activity title?

The requestWindowFeature(Window. FEATURE_NO_TITLE) method of Activity must be called to hide the title.

Where is ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.

What is ActionBar in Android Studio?

The app bar, also known as the action bar, is one of the most important design elements in your app's activities, because it provides a visual structure and interactive elements that are familiar to users.


2 Answers

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().

like image 90
Eugen Pechanec Avatar answered Oct 13 '22 14:10

Eugen Pechanec


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.

like image 42
theapache64 Avatar answered Oct 13 '22 16:10

theapache64