Detecting When User Has Dismissed The Soft Keyboard
I have an EditText widget in my view. When the user selects the EditText widget, I display some instructions and the soft keyboard appears. I use an OnEditorActionListener to dete
Solution 1:
I know a way to do this. Subclass the EditText and implement:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
// Do your thing.returntrue; // So it is not propagated.
}
return super.dispatchKeyEvent(event);
}
Here is a link on how to use your custom views (for when you subclass EditText): http://developer.android.com/guide/topics/ui/custom-components.html
Solution 2:
Jay, your solution is good ! thanks :)
publicclassEditTextBackEventextendsEditText {
private EditTextImeBackListener mOnImeBack;
publicEditTextBackEvent(Context context) {
super(context);
}
publicEditTextBackEvent(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicEditTextBackEvent(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@OverridepublicbooleanonKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
if (mOnImeBack != null)
mOnImeBack.onImeBack(this, this.getText().toString());
}
returnsuper.dispatchKeyEvent(event);
}
publicvoidsetOnEditTextImeBackListener(EditTextImeBackListener listener) {
mOnImeBack = listener;
}
}
publicinterfaceEditTextImeBackListener {
publicabstractvoidonImeBack(EditTextBackEvent ctrl, String text);
}
Solution 3:
I made a slight change on Jay's solution by calling super.onKeyPreIme():
_e = new EditText(inflater.getContext()) {
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
cancelTextInput();
}
return super.onKeyPreIme(keyCode, event);
}
};
Wonderful solution, Jay, +1!
Solution 4:
Here is my custom EditText to detect whether keyboard is showing or not
/**
* Created by TheFinestArtist on 9/24/15.
*/publicclassKeyboardEditTextextendsEditText {
publicKeyboardEditText(Context context) {
super(context);
}
publicKeyboardEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicKeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@OverrideprotectedvoidonFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (listener != null)
listener.onStateChanged(this, true);
}
@OverridepublicbooleanonKeyPreIme(int keyCode, @NonNull KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
&& event.getAction() == KeyEvent.ACTION_UP) {
if (listener != null)
listener.onStateChanged(this, false);
}
returnsuper.onKeyPreIme(keyCode, event);
}
/**
* Keyboard Listener
*/
KeyboardListener listener;
publicvoidsetOnKeyboardListener(KeyboardListener listener) {
this.listener = listener;
}
publicinterfaceKeyboardListener {
voidonStateChanged(KeyboardEditText keyboardEditText, boolean showing);
}
}
Solution 5:
It's 2019 now... So I created a more neat solution with Kotlin
1.Create an extension function:
fun Activity.addKeyboardToggleListener(onKeyboardToggleAction: (shown: Boolean) -> Unit): KeyboardToggleListener? {
val root = findViewById<View>(android.R.id.content)
val listener = KeyboardToggleListener(root, onKeyboardToggleAction)
return root?.viewTreeObserver?.run {
addOnGlobalLayoutListener(listener)
listener
}
}
2.Where the toggle listener is:
openclassKeyboardToggleListener(
privateval root: View?,
privateval onKeyboardToggleAction: (shown: Boolean) -> Unit
) : ViewTreeObserver.OnGlobalLayoutListener {
privatevar shown = falseoverridefunonGlobalLayout() {
root?.run {
val heightDiff = rootView.height - height
val keyboardShown = heightDiff > dpToPx(200f)
if (shown != keyboardShown) {
onKeyboardToggleAction.invoke(keyboardShown)
shown = keyboardShown
}
}
}
}
fun View.dpToPx(dp: Float) = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics).roundToInt()
3.Use it in any Activity as simple as this:
addKeyboardToggleListener {shown ->// hurray! Now you know when the keyboard is shown and hidden!!
}
Post a Comment for "Detecting When User Has Dismissed The Soft Keyboard"