0

short and simple question:

I use the actionBarSherlock library, and wish to have only the app's label set to some string, yet all activities have the empty label unless specified otherwise.

I know you can go over each activity and set its label to "" , but is there a better solution? like using the styles ? I've tried putting :

<item name="android:label"></item>

in there, but it didn't do anything.


EDIT: fact is, for some reason, setting the label of all activities to "" , actually also change the label of the app on some android versions (tested on galaxy s mini, with android 2.3) , so its name is "" too. How come? is it a bug?


This is a bug on either android or any launcher i've tested on.

it seems that setting the label to the activities to "" (or at least the main one) sets the name of the app to "" , so the label of the application tag is just for the default value of all of its activities.

android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

3

You can remove the whole title bar by putting <item name="android:windowNoTitle">true</item> in your app theme. I don't think you should have a title bar without a title (unless on devices running 3.0 or higher, where the title bar becomes the ActionBar and has more functionality), as it'll just waste valuable space.

If you are developing on 3.0+, as well as 2.3, add the following elements to your theme:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<style name="ActionBar" parent="@android:style/Widget.ActionBar">
    <item name="android:displayOptions">showHome|useLogo</item>
</style>

EDIT:

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="android:Theme.Holo">
        <!-- Base Theme style elements -->
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
        <!-- Other style elements. -->
        <item name="android:actionBarStyle">@style/ActionBar</item>
    </style>

    <style name="AppThemeWithTitle" parent="AppTheme">
        <item name="android:actionBarStyle">@style/ActionBarWithTitle</item>
    </style>

    <style name="ActionBar" parent="@android:style/Widget.ActionBar">
        <!-- Other ActionBar style elements. -->
        <item name="android:displayOptions">showHome|useLogo</item>
    </style>

    <style name="ActionBarWithTitle" parent="ActionBar">
        <item name="android:displayOptions">showHome|useLogo|showTitle</item>
    </style>

</resources>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.themes"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.themes.Activity1"
            android:theme="@style/AppThemeWithTitle">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.themes.Activity2" />
        <activity
            android:name="com.example.themes.Activity3" />

    </application>

</manifest>

Activity1 will have the title displayed but Activity2 and Activity3 will not.

William Carter
  • 1,287
  • 12
  • 19
  • that's not what i asked for. i put some stuff on the action bar , like an icon and some action items. sometimes i need to put text and sometimes i don't – android developer May 09 '13 at 13:58
  • Create another theme, say `TitleTheme`, which inherits from `AppTheme`, but with `showTitle`. Then set this as the theme of the relevent activity in your manifest. This way activities will have no title as the default. – William Carter May 09 '13 at 14:10
  • i don't understand. can you please show by code (including manifest)? maybe a sample? – android developer May 09 '13 at 15:22
  • @androiddeveloper, I have added an example of `styles.xml` and `AndroidManifest.xml` in my edit. – William Carter May 09 '13 at 17:01
  • this should work, but it has some flaws: setting the title at runtime will require to re-enable the title, for each theme that i already have, i need to create 2 themes that "extend" it. is there another possible solution? – android developer May 09 '13 at 18:09
  • @androiddeveloper, I'm not sure I understand but if you're worried about having to create two themes for each of your current themes, then this is not the case. Your other themes should be child elements of `AppTheme`, and `AppThemeWithTitle`. Also, if you're setting the title at runtime then you should use just one theme that does not display the title and call `getActionBar().setDisplayShowTitleEnabled(true)` and `getActionBar().setTitle()`. – William Carter May 09 '13 at 21:08
  • but will it also work on older devices? i had a weird thing on android 2.3.6 , which i've set the label for each of the activities to "" and got the app's name changed to it too. also, about the multiple themes, as i've written, i use the actionbarSherlock library, so i might have multiple themes being used. – android developer May 09 '13 at 21:23
  • See [here] (http://stackoverflow.com/questions/2591036/how-to-hide-the-title-bar-for-an-activity-in-xml-with-existing-custom-theme) for dynamically setting the title on devices pre 3.0. You've exhausted my knowledge on the subject though so no more comments from me. Best of luck. – William Carter May 09 '13 at 21:39
  • using your solution worked, but for some reason the actionbar looks weird (unusual background ). i think that i will just do it all programmatically. i just thought there is a quick way to do it, but i see that it's a bug : setting a label in the application tag is actually for all activities, but only the first activity's label is the one the app is actually called by. this means that i was mistaken and it's not just on older devices. it happens even on android 4.2 . it could be a launcher bug too, since i've tested it and you can get the app's real name. – android developer May 12 '13 at 18:19
  • i will mark this answer only because i've found out that it's a bug and i think that any other solution would be as annoying as this. – android developer May 12 '13 at 18:37