How to determine the current IME in Android?
Asked Answered
N

3

16

I have an application where I would like to warn the user if they are not using the default Android softkeyboard. (i.e. they are using Swype or some thing else).

How can I check which input method they currently have selected?

Norahnorbert answered 30/4, 2010 at 13:17 Comment(0)
C
27

You can get a default IME, use:

Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
Cataplexy answered 23/11, 2010 at 13:41 Comment(0)
M
7

InputMethodManager has getEnabledInputMethodList(). You get an InputMethodManager from getSystemService() in your Activity.

As of API 34, InputMethodManager also has a method getCurrentInputMethodInfo() which appears to return the currently selected IME.

Misconception answered 30/4, 2010 at 13:38 Comment(6)
I saw that and that returns a list of input methods that are available to the user - but I can't seem to figure out how to determine which one is currently selected.Norahnorbert
Unfortunately, the only thing I can think to do now is check the length of the List returned by getEnabledInputMethodList and if >2, warn them that they may have a problem if they're not using the default IME. Anyone have any other pointers/ideas?Norahnorbert
You might consider opening up a new SO question about the problem you think you will experience with users using Swype, and figure out how to fix that. After all, I suspect the range of "default IME" will expand as Android moves into other markets (e.g., TVs/set-top boxes). Hence, I would really recommend we figure out how to get it so you don't care what IME they're using.Misconception
Actually - I fixed the problem I was having with swype and a TextWatcher on an EditText However, I still need to determine if they are using the default keyboard or not. (Its for a game where speed of entry matters and you can "Cheat" with swype type keyboards.Norahnorbert
Do you care if they cheat? Unless you're doing multi-player, I don't see why it matters.Misconception
Actually I do - I will have an online score - and am hoping to implement bluetooth head-to-head battles. I'd go further and do online battles, but setting up a networked user matching server is more than a bit beyond me.Norahnorbert
P
2

Here's a bit of code I used to determine if GoogleKeyboard, Samsung Keyboard, or Swype Keyboard is used. The value returned by reflection for mCurId indicates the IME ID.

Test with the different keyboards/input methods you are looking for to find the relevant one

public boolean usingSamsungKeyboard(Context context){
    return usingKeyboard(context, "com.sec.android.inputmethod/.SamsungKeypad");
}

public boolean usingSwypeKeyboard(Context context){
    return usingKeyboard(context, "com.nuance.swype.input/.IME");
}

public boolean usingGoogleKeyboard(Context context){
    return usingKeyboard(context, "com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME");
}   

public boolean usingKeyboard(Context context, String keyboardId)
    {
        final InputMethodManager richImm =
          (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);

        boolean isKeyboard = false;

        final Field field;
        try
        {
            field = richImm.getClass().getDeclaredField("mCurId");
            field.setAccessible(true);
            Object value = field.get(richImm);
            isKeyboard = value.equals(keyboardId);

        }
        catch (IllegalAccessException e)
        {

        }
        catch (NoSuchFieldException e)
        {

        }
        return  isKeyboard;
    }
Paapanen answered 26/8, 2019 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.