7

I want to get the name of my application. How can i get that? Thanks in advance.

Prachi
  • 994
  • 5
  • 23
  • 36

4 Answers4

12

You can use PackageItemInfo -> nonLocalizedLabel to get application name.

val applicationName = context.applicationInfo.nonLocalizedLabel.toString()

[Old Answer]

Usually, we do add the application name with app_name string resource, so you can write below code to access the app name

String applicationName = getResources().getString(R.string.app_name);

Reference : Resource Types

But note that, this resource name is not mandatory and can be changed to any other string name. In that case, the code will not work. See the first code snippet which uses PackageItemInfo -> nonLocalizedLabel above for a better solution.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
5

You can use PackageManager class to obtain ApplicationInfo:

final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
    ai = pm.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {
    ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

EDIT: CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));

This would return the application name as defined in <application> tag of its manifest.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • I tried this code but it's returning the package name not the application name. – Prachi Oct 18 '11 at 05:34
  • what about for this line, CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA)); – user370305 Oct 18 '11 at 05:40
3

you can use PackageManager#getApplicationInfo()

For getting the Application Name for all packages installed in the device. Assuming you have your current Context object ctx

Resources appR = ctx.getResources();
CharSequence txt = appR.getText(appR.getIdentifier("app_name",
"string", ctx.getPackageName()));
bilash.saha
  • 7,226
  • 2
  • 35
  • 40
  • It's returning the package name not the application name. – Prachi Oct 18 '11 at 05:34
  • PackageManager#getApplicationInfo() ? – bilash.saha Oct 18 '11 at 05:37
  • This makes the assumption that you've defined a resource string "app_name" which will typically be the case, but would only be true because that's the default generated by Android Studio. The resource key of the application label can be whatever you want. You should use `PackageManager.loadApplicationLabel()`. – Jeffrey Blattman Jul 08 '19 at 20:08
1

Context has function getString(int resId): Context

so you can use it easily like this.

context.getString(R.string.app_name);
MBH
  • 16,271
  • 19
  • 99
  • 149