136

In my manifest I have:

  <application
    android:name=".MyApp"
    android:icon="@drawable/ic_launcher_icon"
    android:label="@string/app_name"
    android:debuggable="true">

How do I get the label element?

Note: My code is running inside of someone else's, so I don't have access to @string/app_name

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
Johann
  • 27,536
  • 39
  • 165
  • 279

15 Answers15

202

There's an easier way than the other answers that doesn't require you to name the resource explicitly or worry about exceptions with package names. It also works if you have used a string directly instead of a resource.

Just do:

public static String getApplicationName(Context context) {
    ApplicationInfo applicationInfo = context.getApplicationInfo();
    int stringId = applicationInfo.labelRes;
    return stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
}

Edit

In light of the comment from Snicolas, I've modified the above so that it doesn't try to resolve the id if it is 0. Instead it uses, nonLocalizedLabel as a backoff. No need for wrapping in try/catch.

starball
  • 20,030
  • 7
  • 43
  • 238
darrenp
  • 4,265
  • 2
  • 26
  • 22
  • 3
    This works fine, as long as you really use a label in `android:name`. If you hardcoded a string, then it fails. – Snicolas Aug 29 '13 at 15:20
  • 2
    True but the app name is typically specified via a string resource of course. A hard-wired string is flagged by lint and not advised. – darrenp Sep 04 '13 at 18:30
  • 1
    Just beware that it will throw android.content.res.Resources$NotFoundException if the string is not found – zenocon Jan 17 '14 at 15:46
  • As advised by zenocon, better put it inside a `try ... catch`. – superarts.org Feb 26 '16 at 00:48
  • Do you know something about the docu of nonLocalizableLabel?: "...You probably want getApplicationLabel(ApplicationInfo)" – David Sep 03 '19 at 12:53
  • nonLocalizedLabel: The string provided in the AndroidManifest file, if any. You probably don't want to use this. You probably want PackageManager#getApplicationLabel https://developer.android.com/reference/android/content/pm/PackageItemInfo#nonLocalizedLabel – David Sep 27 '21 at 22:44
66

If not mentioned in the strings.xml/hardcoded in AndroidManifest.xml for whatever reason like android:label="MyApp"

Java

public String getAppLable(Context context) {
    ApplicationInfo applicationInfo = null;
    try {
        applicationInfo = context.packageManager.getApplicationInfo(context.getPackageManager().getApplicationInfo().packageName, 0);
    } catch (final NameNotFoundException e) {
        Log.d("TAG", "The package with the given name cannot be found on the system.");
    }
    return (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "Unknown");
}

Or if you know the String resource ID then you can directly get it via

getString(R.string.appNameID);

UPDATE

Kotlin

fun getAppLable(context: Context): String? {
    var applicationInfo: ApplicationInfo? = null
    try {
        applicationInfo = context.packageManager.getApplicationInfo(context.applicationInfo.packageName, 0)
    } catch (e: NameNotFoundException) {
        Log.d("TAG", "The package with the given name cannot be found on the system.")
    }
    return (if (applicationInfo != null) packageManager.getApplicationLabel(applicationInfo) else "Unknown")
}
Vinayak Bevinakatti
  • 40,205
  • 25
  • 108
  • 139
59

Java

public static String getApplicationName(Context context) {
    return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
}

Kotlin (as extension)

fun Context.getAppName(): String = applicationInfo.loadLabel(packageManager).toString()
Boken
  • 4,825
  • 10
  • 32
  • 42
Heath Borders
  • 30,998
  • 16
  • 147
  • 256
32

From any Context use:

getApplicationInfo().loadLabel(getPackageManager()).toString();
zaman
  • 786
  • 7
  • 11
10

In Kotlin its simple:

val appLabel = context.applicationInfo.nonLocalizedLabel.toString()
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
8

In Kotlin, use the following codes to get Application Name:

        // Get App Name
        var appName: String = ""
        val applicationInfo = this.getApplicationInfo()
        val stringId = applicationInfo.labelRes
        if (stringId == 0) {
            appName = applicationInfo.nonLocalizedLabel.toString()
        }
        else {
            appName = this.getString(stringId)
        }
Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
  • nonLocalizedLabel: The string provided in the AndroidManifest file, if any. You probably don't want to use this. You probably want PackageManager#getApplicationLabel https://developer.android.com/reference/android/content/pm/PackageItemInfo#nonLocalizedLabel – David Sep 27 '21 at 22:07
7

If you need only the application name, not the package name, then just write this code.

 String app_name = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
3

You can use this

JAVA

ApplicationInfo appInfo = getApplicationContext().getApplicationInfo();
String applicationLabel = getApplicationContext().getPackageManager().getApplicationLabel(appInfo).toString();
hong developer
  • 13,291
  • 4
  • 38
  • 68
2

Get Appliction Name Using RunningAppProcessInfo as:

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
  ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
  try {
    CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
    Log.w("LABEL", c.toString());
  }catch(Exception e) {
    //Name Not FOund Exception
  }
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
2

By default you have a string resource called "app_name" generated by AndroidStudio. Why not simply use that? Or any other string resource created for this purpose. Much easier than calling several internal methods to come up with a value you have to set yourself in the first place.

Marcell
  • 809
  • 8
  • 23
1

Okay guys another sleek option is

Application.Context.ApplicationInfo.NonLocalizedLabel

verified for hard coded android label on application element.

<application android:label="Big App"></application>

Reference: http://developer.android.com/reference/android/content/pm/PackageItemInfo.html#nonLocalizedLabel

Ahmed Alejo
  • 2,334
  • 2
  • 16
  • 16
1

The source comment added to NonLocalizedLabel directs us now to:

return context.getPackageManager().getApplicationLabelFormatted(context.getApplicationInfo());
Gábor
  • 9,466
  • 3
  • 65
  • 79
1

Kotlin

A simple function to get the name of the application in kotlin

fun getApplicationName() : String{
    var applicationName = ""
    try {
        val applicationInfo = application.applicationInfo
        val stringId = applicationInfo.labelRes
        if (stringId == 0) {
            applicationName = applicationInfo.nonLocalizedLabel.toString()
        }
        else {
            applicationName = application.getString(stringId)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return applicationName
}
Rohit S
  • 714
  • 5
  • 7
0

Have you tried using the PackageManager#getActivityInfo() method? There will be a field that should contain the name of the application.

See the answer to a very similar question here.

Community
  • 1
  • 1
gkiar
  • 480
  • 1
  • 6
  • 20
0

If "don't have access" means you don't get the expected value... Try This:

String appName = getString(R.string.app_name);
vimuth
  • 5,064
  • 33
  • 79
  • 116