Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override Soft keyboard keyPress event in android?

I need a listener's to detect user interaction on my activity which has a fragment attached to it, for which i have overriden diapatchTouchEvent() which is working fine for touch event and hard key events, but it does not work for soft keyboard keypress.

To intercept keyboard touch events i have tried onUserInteraction(), dispatchKeyEvents(), onKeyUp, onKeyDown but none of them is working for softkeyboard.

Is there any other way to intercept softkeyboard events in Android?

for above query i have gone through these links :

https://stackoverflow.com/a/25620981/3954050

Android SoftKeyboard onKeyDown/Up not detecting 'alternative' keys

like image 420
Tushar Purohit Avatar asked Dec 01 '25 00:12

Tushar Purohit


1 Answers

If you want to intercept keyboard event like done, or search or enter etc, in your edit text add a imeOption like

<EditText
   ..../
  android:id="@+id/myEditText"
  android:imeOptions="actionSearch"/>

And add an EditorActionListener to that edit text.

myEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            if (actionId == EditorInfo.IME_ACTION_SEARCH){
                //Do something
            }
like image 80
Shashank Udupa Avatar answered Dec 02 '25 13:12

Shashank Udupa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!