Skip to content

Instantly share code, notes, and snippets.

@vxhviet
Created April 12, 2019 06:25
Show Gist options
  • Save vxhviet/f2e97e9b5568c7e9f8f8f38d80d2a769 to your computer and use it in GitHub Desktop.
Save vxhviet/f2e97e9b5568c7e9f8f8f38d80d2a769 to your computer and use it in GitHub Desktop.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment