Is there a possibility in Android (API 24 - 29) to get the human-readable name of the current default keyboard? When I use the following code
String keyboard = Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
I'm getting
com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME
But I would like to have Gboard instead (i.e. the name that is displayed in the keyboard selection menu and not the package name).
That result is the String form of a ComponentName, so we can use the unflattenFromString() method to easily parse out the package name, and then retrieve the package's label – i.e., the human-readable name – from its ApplicationInfo obtained with PackageManager. For example, in a simple Java utility method:
public static CharSequence getCurrentImeLabel(Context context) {
CharSequence readableName = null;
String keyboard = Settings.Secure.getString(context.getContentResolver(), DEFAULT_INPUT_METHOD);
ComponentName componentName = ComponentName.unflattenFromString(keyboard);
if (componentName != null) {
String packageName = componentName.getPackageName();
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo info = packageManager.getApplicationInfo(packageName, 0);
readableName = info.loadLabel(packageManager);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
return readableName;
}
And an equivalent Kotlin extension on Context:
fun Context.getCurrentImeLabel() : CharSequence? {
val keyboard = Settings.Secure.getString(contentResolver, DEFAULT_INPUT_METHOD)
return ComponentName.unflattenFromString(keyboard)?.let {
packageManager.getApplicationInfo(it.packageName, 0).loadLabel(packageManager)
}
}
If all you need is that human-readable name, then this seems to be the most direct approach, as InputMethodManager doesn't appear to have any public method that returns the current IME.
However, if you need additional IME-specific information, it seems that it will have to be pulled from the List<InputMethodInfo> returned from InputMethodManager's getInputMethodList() or getEnabledInputMethodList() methods. In both cases, you would need to iterate over the List, checking for which InputMethodInfo#getId() equals that String returned from Settings.
The InputMethodInfo class also has a loadLabel(PackageManager) method available, so if you're using this method, or otherwise have the necessary InputMethodInfo already, then you can use that directly, rather than making an unnecessary getApplicationInfo() call.
I have an activity name from a third party application
"com.example.packagename.MainActivity"
How can I retrieve programmatically the package name of this third party application?
PS: I am not even sure this is an Activity. It could be a wrong string.
You can find the package name from an external app:
private String getExternalPackageName(String appname) {
PackageManager pm = thisActivity.getPackageManager();
List<ApplicationInfo> aList = pm.getInstalledApplications(PackageManager.GET_META_DATA);
String aName = "";
for (ApplicationInfo appi : aList) {
String n = (String)pm.getApplicationLabel(appi);
if (n.toUpperCase().contains(appname)) {
aName = appi.packageName;
}
}
return aName;
}
be sure to call this function with appname => uppercase
I wish to get name/packageName of current app using my customKeyboard service.
Is there anyway to do so?
You have to rely on getCurrentInputEditorInfo(), a method in InputMethodService, to achieve this:
String packageName = getCurrentInputEditorInfo().packageName;
Found answer in this topic: How to check current running applications in Android?
If app is running its obviosuly the one calling my keyboard.
private String getApplicationName() {
final PackageManager pm = mActivity.getApplicationContext()
.getPackageManager();
ApplicationInfo ai;
String appName;
try {
ai = pm.getApplicationInfo(mActivity.getPackageName(), 0);
appName = (String) pm.getApplicationLabel(ai);
} catch (final NameNotFoundException e) {
appName = "(unknown)";
}
return appName;
}
use this method to get Appname/Package name
I am getting the names for my installed applications with below code and use them to see if any updates for this application is available.
But sometimes an incorrect name (MX Speler instead of MX Player) is being provided, as a result no updates are found.
Is there any better code i can/should use?
{
final PackageInfo pi = installedInfo != null ? installedInfo : downloadedInfo;
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(pi ??, 0); //How to set the name of the installed application?
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
System.out.println("Application name : "+ applicationName);
}
You should not use labels as it may be different for every language or changed on each update. You should only rely on application's id (packageId) as this id stays unchanged for the whole life of the application.
In my manifest I have:
<application
android:name=".MyApp"
android:icon="#drawable/ic_launcher_icon"
android:label="#string/app_name"
android:debuggable="true">
How do I get the label element?
Note: My code is running inside of someone else's, so I don't have access to #string/app_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);
}
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"
Java
public String getAppLable(Context context) {
ApplicationInfo applicationInfo = null;
try {
applicationInfo = context.packageManager.getApplicationInfo(context.getPackageManager().getApplicationInfo().packageName, 0);
} catch (final NameNotFoundException e) {
Log.d("TAG", "The package with the given name cannot be found on the system.");
}
return (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);
UPDATE
Kotlin
fun getAppLable(context: Context): String? {
var applicationInfo: ApplicationInfo? = null
try {
applicationInfo = context.packageManager.getApplicationInfo(context.applicationInfo.packageName, 0)
} catch (e: NameNotFoundException) {
Log.d("TAG", "The package with the given name cannot be found on the system.")
}
return (if (applicationInfo != null) packageManager.getApplicationLabel(applicationInfo) else "Unknown")
}
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()
From any Context use:
getApplicationInfo().loadLabel(getPackageManager()).toString();
In Kotlin its simple:
val appLabel = context.applicationInfo.nonLocalizedLabel.toString()
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 need only the application name, not the package name, then just write this code.
String app_name = packageInfo.applicationInfo.loadLabel(getPackageManager()).toString();
You can use this
JAVA
ApplicationInfo appInfo = getApplicationContext().getApplicationInfo();
String applicationLabel = getApplicationContext().getPackageManager().getApplicationLabel(appInfo).toString();
Get Appliction 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
}
}
By default you have a string resource called "app_name" generated by AndroidStudio. Why not simply use that? Or any other string resource created for this purpose. Much easier than calling several internal methods to come up with a value you have to set yourself in the first place.
Okay guys another sleek option is
Application.Context.ApplicationInfo.NonLocalizedLabel
verified for hard coded android label on application element.
<application android:label="Big App"></application>
Reference:
http://developer.android.com/reference/android/content/pm/PackageItemInfo.html#nonLocalizedLabel
The source comment added to NonLocalizedLabel directs us now to:
return context.getPackageManager().getApplicationLabelFormatted(context.getApplicationInfo());
Kotlin
A simple function to get the name of the application in kotlin
fun getApplicationName() : String{
var applicationName = ""
try {
val applicationInfo = application.applicationInfo
val stringId = applicationInfo.labelRes
if (stringId == 0) {
applicationName = applicationInfo.nonLocalizedLabel.toString()
}
else {
applicationName = application.getString(stringId)
}
} catch (e: Exception) {
e.printStackTrace()
}
return applicationName
}
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.
If "don't have access" means you don't get the expected value... Try This:
String appName = getString(R.string.app_name);