In my application I need to make the user choose an input method. Once it's selected I should perform some task. How is detected that the user actually chooses an InputMethod
?
This is the code used to show the InputMethod
list.
InputMethodManager imeManager = (InputMethodManager) mw.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imeManager != null) {
imeManager.showInputMethodPicker();
} else {
Toast.makeText(mw.getApplicationContext(), "IME ERROR",
Toast.LENGTH_LONG).show();
}
Unfortunately you cannot catch the input method which user picks in InputMethodPicker
.
However, you can check it after user picks it, using BroadcastReceiver
.
When IME changes, Intent.ACTION_INPUT_METHOD_CHANGED
will be broadcasted.
public class InputMethodChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_INPUT_METHOD_CHANGED)) {
.....
/* You can check the package name of current IME here.*/
}
}
}
Then, register it.
IntentFilter filter = new IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED);
registerReceiver(mReceiver, filter);
Well, even though you accepted the answer, there is a way to check if user selects your keyboard as a default:
public static boolean isThisKeyboardSetAsDefaultIME(Context context) {
final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
return isThisKeyboardSetAsDefaultIME(defaultIME, context.getPackageName());
}
public static boolean isThisKeyboardSetAsDefaultIME(String defaultIME, String myPackageName) {
if (TextUtils.isEmpty(defaultIME))
return false;
ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
if (defaultInputMethod.getPackageName().equals(myPackageName)) {
return true;
} else {
return false;
}
}
You can combine this code with code from this answer and once user selects keyboard from InputMethodPicker check if your keyboard is a default (meaning that user selected it).
If you have more questions about implementing a keyboard, check this project. Cheers.
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