Detect if Input Method has been selected
Asked Answered
C

3

9

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();
}
Costplus answered 28/8, 2013 at 11:4 Comment(2)
"In my application I need to make the user choose an input method" -- why?Bohol
@Bohol because it handles behavior of a specific input device.Costplus
D
21

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);
Dam answered 28/8, 2013 at 12:31 Comment(3)
Thank you, it's enough for what I need!Costplus
Unfortunately the context.packageName always just returns your own package name, so this doesn't work.Dado
Figured it out. Here's how to get the selected IME: Settings.Secure.getString(ctx?.contentResolver, Settings.Secure.DEFAULT_INPUT_METHOD)Dado
P
7

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.

Psychognosis answered 9/10, 2015 at 22:56 Comment(0)
K
0

i am doing this way to detect if my ime is selected or enabled

for checking isSelected

 public static boolean isThisKeyboardSelected(Context context) {
        final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
        if (TextUtils.isEmpty(defaultIME))
            return false;
        ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
//        return defaultInputMethod.getPackageName().equals(myPackageName);
        return defaultIME.contains("your package Name here");

    }



     public static boolean isThisKeyboardSetAsDefaultIME(Context context) {
        final String defaultIME = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_INPUT_METHODS);
        if (TextUtils.isEmpty(defaultIME))
            return false;
        ComponentName defaultInputMethod = ComponentName.unflattenFromString(defaultIME);
//        return defaultInputMethod.getPackageName().equals(myPackageName);
        return defaultIME.contains("your package name);

    }
Kinematograph answered 19/4, 2018 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.