Search code examples
androidandroid-actionbarandroid-titlebar

How to change the launcher activity name and icon and keep the app name and icon?


I would like to have different title bar icons and names in the different activities, but not to change the app name or icon. The first activity should not have a title name.

This is what I have in the AndroidManifest.xml:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="" <!-- there should be no title here -->
            android:icon="@drawable/custom_icon"
            android:windowSoftInputMode="stateHidden|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:screenOrientation="portrait"
            android:name=".MainActivity"
            android:icon="@drawable/another_custom_icon"
            android:label="MainTitle" >
        </activity>
</application>

Apparently Android uses the name and the icon of the first activity for the app name and icon. I know that I can put this line of code in the onCreate of the activity:

    getActionBar().setTitle("");

But it is also shown during load, until it gets removed.

This is the styles.xml file:

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/actionBarCustomization</item>
</style>

<style name="actionBarCustomization" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#FFFFFF</item>
    <item name="android:titleTextStyle">@style/actionBarTextColor</item>
</style>

<style name="actionBarTextColor" parent="@android:style/TextAppearance">
    <item name="android:textColor">#000000</item>
</style>

Solution

  • AndroidManifest.xml

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
           <!-- This the name on Activity so put there the name of your MainActivity i.e com.example.MainActivity -->
            android:name="NAME OF YOU LAUNCHER ACTIVITY"       
            android:icon="@drawable/custom_icon"
            android:theme="@style/Theme.NoActionBarTitle"
            android:windowSoftInputMode="stateHidden|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:screenOrientation="portrait"
            android:name=".MainActivity"
            android:icon="@drawable/another_custom_icon"
            android:label="MainTitle" >
        </activity>
    

    Style.xml

    <style name="AppTheme" parent="@android:style/Theme.Holo.Light">
        </style>
    <style name="Theme.NoActionBarTitle" parent="@android:style/Theme.Holo.Light">
      <item name="android:actionBarStyle">@style/NoActionBarTitle.ActionBar</item>
    </style>
    
     <style name="NoActionBarTitle.ActionBar"    parent="@android:style/Widget.Holo.Light.ActionBar">
      <<item name="android:displayOptions">showHome|useLogo</item>
     </style>
    

    After That you can show the title of activity if you want to By using method.

     getActionBar().setDisplayShowTitleEnabled(true);
    

    For Those who are using appcompat support library.I mean there activites are extending from AppCompatActivity use the below style file

    Style.xml if you are using appcompat

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
    
    <style name="Theme.NoActionBarTitle" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarStyle">@style/NoActionBarTitle.ActionBar</item>
        <item name="android:actionBarStyle">@style/NoActionBarTitle.ActionBar</item>
    </style>
    
    <style name="NoActionBarTitle.ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:displayOptions">showHome|useLogo</item>
        <item name="displayOptions">showHome|useLogo</item>
    </style>