views:

53

answers:

2

Hi,

In my manifest file, I have given the application name as MyApp and the name of the starting activity as Main Menu.

<application android:theme="@style/theme" android:icon="@drawable/myicon" android:label="@string/app_name">
    <activity android:name=".MainMenu"
              android:label="@string/mainmenu_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

When I see the app icon in the default menu screen of android, the name shown is Main Menu. Why is MyApp not the name shown for the app?

-Kiki

A: 

If android:label is set for activity then its value is taken. It it is omitted for activity then the android:label attribute set on application is taken instead.

You simply cannot have different labels for activity and for application as your application has one launcher for your activity with the label you set it to have. I'm not quite sure I totally understand what you are trying to achieve tho.

Octavian Damiean
+3  A: 

Because your app can define more than one activity that shows up in the launcher. All activities that have this type of intent filter:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Will appear in the launcher, each with its own label. The fact that if you don't define a label on your activity, it's inherited from the application is basically a commodity.


Also, every activity has its own android:icon (which, again, is inherited from <application> for commodity). With this property your two launcher entries can have not only different labels, but different icons as well.


On a more general note, even "non-main" activities can handle certain types of events, and in these cases their labels and icons are relevant. For example, if one of your activities (doesn't matter which one) handles ACTION_SEND intents (like most sharing apps do -- see Facebook, Twitter, ...), then whenever the user presses a "Share" button in any application, a list of applications that can complete that certain activity is displayed to the user (in this case these applications could be Facebook, Twitter and your app). This list will contain an icon and a label (i.e. Facebook logo Facebook). Your entry's display will be dictated by the android:icon and android:label attributes defined by your receiving <activity>, and only if it doesn't define them they'll be taken from your <application>.

This allows you to have an entry such as "Share via MyApp" without modifying your application's name. Or, if your application also has an activity that handles ACTION_VIEW, that activity could have, for example, the label "View in MyApp".


Digging even deeper, <intent-filter> elements can have their own label and icon. Basically, Android chooses the icon and label to display like this:

  • If the <intent-filter> has it, pick that, stop.
  • If the <activity> has it, pick that, stop.
  • Pick the one in <application>.

This is helpful in two situations:

  1. You don't want to change your activity's label / icon: maybe you want to use those in other places such as the title bar (and you don't want your title bar to say "Share via MyApp")
  2. You have activities with multiple intent filters. For example, you could handle both ACTION_SEND and ACTION_VIEW intents in the same activity. In this case, you'd have two intent filters set for that activity with the labels "Share via MyApp" and "View in MyApp".

I hope I've made myself clear, these are very basic things that can be pretty hard to grasp at first, but that provide great flexibility in building apps and integrating them with the Android system and/or other apps.


Another thing to note is that you can easily have an application that doesn't have any entries in the launcher (by not having any activity with the intent filter mentioned above). These are perfectly valid apps and it's good practice when creating Service-only apps (such as widgets).

Felix