139

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

0

15 Answers 15

209

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.

7
  • 4
    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, 2013 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 4, 2013 at 18:30
  • 1
    Just beware that it will throw android.content.res.Resources$NotFoundException if the string is not found
    – zenocon
    Jan 17, 2014 at 15:46
  • As advised by zenocon, better put it inside a try ... catch. Feb 26, 2016 at 0:48
  • Do you know something about the docu of nonLocalizableLabel?: "...You probably want getApplicationLabel(ApplicationInfo)"
    – David
    Sep 3, 2019 at 12:53
67

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")
}
2
  • while reading App Name from string.xml, sometimes, am getting junk characters. I dont know why ..Ex: सà¥&#141;à¤&#149;à¥&#136;नर Oct 17, 2016 at 9:41
  • why do you get application info from application info? omg... context.packageManager.getApplicationInfo(context.applicationInfo.packageName, 0)
    – user924
    Dec 21, 2023 at 14:07
66

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()
2
  • 3
    loadLabel returns a CharSequence. I think you need .toString() on the end.
    – joshbodily
    Mar 22, 2017 at 23:23
  • return is CharSequence or String.valueOf(); Apr 24, 2017 at 2:57
32

From any Context use:

getApplicationInfo().loadLabel(getPackageManager()).toString();
0
10

In Kotlin its simple:

val appLabel = context.applicationInfo.nonLocalizedLabel.toString()
1
  • 1
    nonLocalizedLabel.toString()
    – David
    Sep 27, 2021 at 22:42
8

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

 String app_name = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
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)
        }
1
3

You can use this

JAVA

ApplicationInfo appInfo = getApplicationContext().getApplicationInfo();
String applicationLabel = getApplicationContext().getPackageManager().getApplicationLabel(appInfo).toString();
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
  }
}
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.

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

1

The source comment added to NonLocalizedLabel directs us now to:

return context.getPackageManager().getApplicationLabelFormatted(context.getApplicationInfo());
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
}
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.

0

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

String appName = getString(R.string.app_name);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.