My app opens the input method picker (the menu where you choose a keyboard) with InputMethodManager.showInputMethodPicker()
. My app doesn't actually create the picker (it's created by InputMethodManager) but I know it's a ContextMenu
and its id is R.id.switchInputMethod
.
The picker is part of a multi-step wizard so I need to know when the picker closes so my app can proceed to the next step. Right now I'm checking in a background thread if the default keyboard changed, but that doesn't help if the user selects the same keyboard or presses back.
So I need a way to tell when the picker closes (or some other clever way to know when to proceed).
Thanks in advance...
Here is a small trick. Please do test it and let us know if it works.
All you have to do is make your activity extend this InputMethodActivity. When you need the user to pick input method, call pickInput(), and onInputMethodPicked() will be called when the user is done.
package mobi.sherif.inputchangecheck;
import android.os.Bundle;
import android.os.Handler;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.inputmethod.InputMethodManager;
public abstract class InputMethodActivity extends FragmentActivity {
protected abstract void onInputMethodPicked();
@Override
protected void onCreate(Bundle savedInstanceState) {
mState = NONE;
super.onCreate(savedInstanceState);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(mState == PICKING) {
mState = CHOSEN;
}
else if(mState == CHOSEN) {
onInputMethodPicked();
}
}
private static final int NONE = 0;
private static final int PICKING = 1;
private static final int CHOSEN = 2;
private int mState;
protected final void pickInput() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showInputMethodPicker();
mState = PICKING;
}
}
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