-1

I want to handle "dismiss soft keyboard" event in Android, and as far as I understood, the only solution to do this is to subclass EditText which I did according to this answer.

Here is my class:

public class EditTextCustom extends EditText {

    public EditTextCustom(Context context) {
        super(context);
    }

    public EditTextCustom(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextCustom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (listener != null)
            listener.onStateChanged(this, true);
    }

    @Override
    public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            Log.d("Info", "Soft keyboard was hidden");
            if (listener != null) {
                listener.onStateChanged(this, false);
            }
        }
        return super.onKeyPreIme(keyCode, event);
    }

    KeyboardListener listener;

    public void setOnKeyboardListener(KeyboardListener listener) {
        this.listener = listener;
    }

    public interface KeyboardListener {
        void onStateChanged(EditTextCustom keyboardEditText, boolean showing);
    }
}

I need this event(s) (KeyEvent.KEYCODE_BACK and KeyEvent.ACTION_UP) to act as EditorInfo.IME_ACTION_DONE, that is when user dismisses soft keyboard, it is interpreted by Android if it has pressed "Done" and the edits to the EditText were applied.

Best practice to implement this?

Community
  • 1
  • 1
Neurotransmitter
  • 6,289
  • 2
  • 51
  • 38

1 Answers1

-1
mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // do something, e.g. set your TextView here via .setText()
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });

mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // do something, e.g. set your TextView here via .setText()
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });

and in xml

android:imeOptions="actionDone"

This may help you.

Jayamurugan
  • 542
  • 2
  • 10