Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Input Method has been selected

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();
}
like image 354
Vektor88 Avatar asked Aug 28 '13 11:08

Vektor88


2 Answers

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);
like image 136
Zhenghong Wang Avatar answered Sep 17 '22 04:09

Zhenghong Wang


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.

like image 30
Fedor Tsyganov Avatar answered Sep 20 '22 04:09

Fedor Tsyganov