13

http://developer.android.com/guide/topics/text/creating-input-method.html#GeneralDesign reads:

Because multiple IMEs may be installed on the device, provide a way for the user to switch to a different IME directly from the input method UI.

Let's assume I have the source of two input methods and can modify it. I want to let the user switch between them quickly and am ready to reserve a button for that. How do I "switch to a different IME directly from the input method UI"?

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127

3 Answers3

18

Switching to the previous input method from the current input method is:

//final String LATIN = "com.android.inputmethod.latin/.LatinIME";
// 'this' is an InputMethodService
try {
    InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
    final IBinder token = this.getWindow().getWindow().getAttributes().token;
    //imm.setInputMethod(token, LATIN);
    imm.switchToLastInputMethod(token);
} catch (Throwable t) { // java.lang.NoSuchMethodError if API_level<11
    Log.e(TAG,"cannot set the previous input method:");
    t.printStackTrace();
}

If you want to switch to a particular input method whose ID you know, you may do as the commented-out lines suggest.

EDIT @pRaNaY suggested a single .getWindow() in a silent edit (click "edited" below to see the history). I remember that it did not work for Android 2.3; if you consult the docs, you will see that the first call, InputMethodService.getWindow() returns a Dialog (which is not a subclass of Window), and the second call, Dialog.getWindow() returns a Window. There is no Dialog.getAttributes(), so with a single .getWindow() it will not even compile.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • but how to get instance of service class for final IBinder token = this.getWindow().getWindow().getAttributes().token; – Ram May 25 '15 at 06:55
  • @Ram if your project is an InputMethodService, and it is currently selected by the user, this is your service. – 18446744073709551615 May 25 '15 at 10:00
  • I'm confused, doesn't this solution contradict what @Raghav Sood was saying - that for security reasons this is not allowed? – Ronen Rabinovici Mar 07 '16 at 11:46
  • @RonenRabinovici the security check is that _`setInputMethod()` can only be called from an application or a service which has a token of the currently active input method._ – 18446744073709551615 Mar 13 '16 at 04:01
12

You cannot change the user's currently active IME through code for security reasons, sorry.

However, you can show a system provided dialog to allow the user to select one of the other enabled ones.

InputMethodManager imeManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
if (imeManager != null) {
    imeManager.showInputMethodPicker();
} else {
    Toast.makeText(context ,"Error", Toast.LENGTH_LONG).show();
}
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
0

If you have rooted device, you can use /system/bin/ime utility.

List all installed input methods: # ime list -a

Set google's keyboard as default:

ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME
i love jack
  • 119
  • 3
  • 9