I want to detect when the soft keyboard is show and hidden. For this i do like this :
public class ALEditText extends EditText {
private ALSoftInputListener mSoftInputListener;
private static class SoftInputReceiver extends ResultReceiver {
private static final int RESULT_UNCHANGED_HIDDEN = 1;
private static final int RESULT_SHOWN = 2;
private static final int RESULT_HIDDEN = 3;
private static final int RESULT_UNCHANGED_SHOWN = 0;
private ALSoftInputListener mListener;
public SoftInputReceiver(ALSoftInputListener listener) {
super(null);
this.mListener = listener;
}
public void onReceiveResult(int result, Bundle data) {
switch (result) {
case RESULT_UNCHANGED_SHOWN /*0*/:
case RESULT_SHOWN /*2*/:
if (this.mListener != null) {
this.mListener.onSoftInputShown();
}
case RESULT_UNCHANGED_HIDDEN /*1*/:
case RESULT_HIDDEN /*3*/:
if (this.mListener != null) {
this.mListener.onSoftInputHidden();
}
default:
}
}
}
public void showSoftInput() {
SoftInputReceiver receiver = new SoftInputReceiver(this.mSoftInputListener);
InputMethodManager imm = getInputMethodManager();
imm.showSoftInput(this, 0, receiver);
}
public void HideSoftInput() {
SoftInputReceiver receiver = new SoftInputReceiver(this.mSoftInputListener);
InputMethodManager imm = getInputMethodManager();
imm.hideSoftInputFromWindow(getWindowToken(), 0, receiver);
}
public void SetSoftInputListener(ALSoftInputListener listener) {
this.mSoftInputListener = listener;
}
protected InputMethodManager getInputMethodManager() {
return (InputMethodManager) getContext().getSystemService("input_method");
}
}
but i have a strange behavior that i can't understand, when i do showSoftInput then the virtual keyboard is show and the event onSoftInputShown is also raised BUT it's immediately followed by onSoftInputHidden and the keyboard is still visible! later if i hide the keyboard the event onSoftInputHidden will be not call again ... does someone can explain me what is happening ?