Prachi
Prachi

Reputation: 1004

How to get the name of the application in android?

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

Upvotes: 7

Views: 18700

Answers (4)

Pankaj Kumar
Pankaj Kumar

Reputation: 83028

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.

Upvotes: 12

MBH
MBH

Reputation: 16639

Context has function getString(int resId): Context

so you can use it easily like this.

context.getString(R.string.app_name);

Upvotes: 1

user370305
user370305

Reputation: 109257

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.

Upvotes: 5

bilash.saha
bilash.saha

Reputation: 7316

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

Upvotes: 3

Related Questions