vxh.viet
4/12/2019 - 6:25 AM

Listen for key Done, Search on EditText

Listen for key Done, Search on EditText

SOURCE

Listen for Done, Search keypress on EditText (work for all device):

<EditText
  ...
  android:imeOptions="actionSearch"
  ...
fpc_promo_code_input_edt.setOnEditorActionListener(object: TextView.OnEditorActionListener {
    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            KeyboardUtils.hideSoftKeyboard(activity)
            return true
        }
        return false
    }
})


public static void hideSoftKeyboard(Activity activity) {
    try {
        final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            final View focusView = activity.getCurrentFocus();
            if (focusView != null) {
                imm.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
                focusView.clearFocus();
            } else {
                if (isKeyboardShow(activity)) {
                    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
                }
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

Similar for key DONE.