Android - How to find out the name of the application? (Not package name)

In my manifest, I have:

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

How to get a label item?

Note. My code runs inside someone else's code, so I do not have access to @ string / app_name

+100
android
Jun 27 '12 at 15:00
source share
11 answers

It is easier than other answers that do not require explicit resource specification or concern about exceptions with package names. It also works if you used the string directly and not 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 modified the above so that it does not try to resolve the identifier if it is 0. Instead, it uses nonLocalizedLabel as a deferment. There is no need for a wrapper in try / catch.

+153
Feb 27 '13 at 14:23
source share

Unless specified in the strings.xml / hardcoded file in AndroidManifest.xml for any reason, such as 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 identifier, you can directly get it through

 getString(R.string.appNameID); 
+50
May 08 '13 at
source share
 public static String getApplicationName(Context context) { return context.getApplicationInfo().loadLabel(context.getPackageManager()); } 
+34
May 10 '13 at 16:17
source share

From any use of context:

 getApplicationInfo().loadLabel(getPackageManager()).toString(); 
+20
Sep 11 '14 at 7:10
source share

If you know the package name, use the following snippet

 ApplicationInfo ai; try { ai = pm.getApplicationInfo(packageName, 0); } catch (final NameNotFoundException e) { ai = null; } final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)"); 
+11
Jun 27 '12 at 15:03
source share

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

  String app_name = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString(); 
+6
Jul 15 '14 at 9:19
source share

Get application 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 } } 
+1
Jun 27 '12 at 15:09
source share

Ok guys, another smooth option is

Application.Context.ApplicationInfo.NonLocalizedLabel

Checked for Android hard coded label in application element.

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

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

+1
Jan 24 '16 at 22:08
source share

In Kotlin, use the following codes to get the 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
Apr 27 '19 at 7:42
source share

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
Jun 27 '12 at 15:04
source share

The original comment added to NonLocalizedLabel directs us to:

 return context.getPackageManager().getApplicationLabelFormatted(context.getApplicationInfo()); 
0
Oct 10 '18 at 20:56
source share



All Articles