3

I added an action bar with a dropdown menu to my activity but it also shows the application name.

Each of my activities has an action bar and each of them use the theme @android:style/Theme.Holo.Light. One one my activity screens I would like to display the action bar with the dropdown/spinner menu but I would like to hide the application title.

I saw this solution on another SO post but it required me changing the global theme settings and if I've understood correctly, that approach would remove the title from all my action bars.

How can I do this?


Here's a screenshot:

enter image description here

Community
  • 1
  • 1
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382

4 Answers4

12

Can be done with a nice one liner

getActionBar().setDisplayShowTitleEnabled(false)

this will hide just the title. If you also want to hide the home logo

getActionBar().setDisplayShowHomeEnabled(false);

When using the AppCompat Support library, you should replace getActionBar() with getSupportActionBar(). It is also suggested that you check if the ActionBar is null before changing values on it.

ActionBar actionBar = getSupportActionBar()
if (actionBar != null) {
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
}
dsrees
  • 6,116
  • 2
  • 26
  • 26
  • @Drdavidpier depends on state. Look into the Android Lifecycle. there are states in which the actionbar isn't created yet – dsrees Aug 10 '15 at 18:42
6

For this kind of simple stuff you just have to go to the source to find the answer: here the full explanation: http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown

and here some guide code, to include the spinner on the action bar:

 ActionBar actionBar = getActionBar();
 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

after that is just put a listener for the changes and setting the a spinner adapter.

and you can use:

 setTitle("");

to remove the title. I've been checking on my codes here, you can also give a try on:

 requestWindowFeature(Window.FEATURE_NO_TITLE);

It should just remove the title, and leave the title bar. Remember that you must call this before setContentView

happy coding.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Hi Budius. That's what I've done in my code. It shows me the dropdown but it also shows me the application name. I would only like the dropdown/spinner. – Mridang Agarwalla Aug 16 '12 at 10:55
  • I've added a screenshot to explain better. – Mridang Agarwalla Aug 16 '12 at 11:00
  • That's very weird cause I just finished an app with NAVIGATION_MODE_TAB and it removed, i was expecting the same behavior. You can try to `setTitle("");` – Budius Aug 16 '12 at 11:06
  • `setTitle("")` did it but I'm wondering if this is a correct way of accomplishing this. Could you edit your answer so I can accept it. Thanks. – Mridang Agarwalla Aug 16 '12 at 11:35
  • @MridangAgarwalla hi.. I just edited and even suggested something else that looks more "proper way", I'm not sure if it works but it's an easy test. – Budius Aug 16 '12 at 14:15
1

The above proposed solutions still show the title for a brief moment before making it invisible. I use the following which I believe is the best way to achieve this:

Simple disable app_name in the activity declaration in the Manifest. This way it will never show for the activity where you don't want it to appear:

<activity
        android:name="com.myapp.MainActivity"
        android:label="@string/app_name" > <!-- Delete this -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
zeeshan
  • 4,913
  • 1
  • 49
  • 58
-4

use this in your activity tag of menifeast

android:theme="@android:style/Theme.NoTitleBar"
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75