Method 1 – From the Play Store As I mentioned, the Play Store also uses the Android app package name to list unique apps. So, the easiest way to find the app package name is via the Play Store. On a PC/Mac: Open play.google.com in your web browser.
You can find an app's package name in the URL of your app's Google Play Store listing. For example, the URL of an app page is play.google.com/store/apps/details? id=com.
The applicationId exactly matches the Java-style package name you chose during project setup in android studio. However, the application ID and package name are independent of each other beyond this point.
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);
public static String getApplicationName(Context context) {
return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
}
fun Context.getAppName(): String = applicationInfo.loadLabel(packageManager).toString()
From any Context use:
getApplicationInfo().loadLabel(getPackageManager()).toString();
If you know Package name then Use 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)");
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With