Android - How to get application name? (Not package name) Android - How to get application name? (Not package name) android android

Android - How to get application name? (Not package name)


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);}

Hope this helps.

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.


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

public String getAppLable(Context context) {    PackageManager packageManager = context.getPackageManager();    ApplicationInfo applicationInfo = null;    try {        applicationInfo = packageManager.getApplicationInfo(context.getApplicationInfo().packageName, 0);    } catch (final NameNotFoundException e) {    }    return (String) (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);


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()