sagar suri
sagar suri

Reputation: 4731

Unable to detect delete button of soft keyboard in emulator

I am trying to detect back or delete button of soft keyboard using below method. Its working perfectly in physical devices but its not working emulator. Emulator is able to detect all the key events(like enter button, number buttons) expect delete button. On pressing the delete button in emulator neither onKeyDown() nor dispatchKeyEvent() is called. What can be the problem ?

Purpose of doing this:

I am trying to navigate to previous EditText fields if the user presses delete on the current empty EditText field. That is why I was relying on the dispatchKeyEvent() method.

I am using Nexus 5X API 25 emulator

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DEL) {
            Log.e(TAG,keyCode)
        }
        return super.onKeyDown(keyCode, event);
    }

Here is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:hint="Name" />

</RelativeLayout>

Image of the delete button:

enter image description here

Upvotes: 1

Views: 1905

Answers (1)

Vikasdeep Singh
Vikasdeep Singh

Reputation: 21766

After understanding your requirement I found that onKeyDown and onKeyUp are not working on Nexus 5 API 25 emulator but TextWatcher can serve your purpose if you really want to use Nexus 5 API 25 emulator.

On Nexus 5X API 26, onKeyDown and onKeyUp are working fine. Of course TextWatcher is working as well.

Use TextWatcher to detect change in text inside EditText and there you can check which key is pressed. Below is working code:

    TextWatcher textWatcher = new TextWatcher() {

        public void  beforeTextChanged(CharSequence s, int start, int count, int after){
            if (after < count) {
                Log.d("DELETE_KEY_PRESSED", "beforeTextChanged: ");
            }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length()>0 && s.subSequence(s.length()-1, s.length()).toString().equalsIgnoreCase("\n")) {
                Log.d("ENTER_KEY_PRESSED", "onTextChanged: ");
            } else if (s.length() == 0) {
                Log.d("DELETE_KEY_PRESSED", "onTextChanged: ");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    EditText editText = (EditText) findViewById(R.id.edit);
    editText.addTextChangedListener(textWatcher);

You can use TextWatcher to detect all the other key pressed as well.

By the way, for me onKeyDown() and onKeyUp() are also working fine on emulator. I am using below code:

//To detect DELETE KEY PRESS event there is no text in EditText
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_DEL) {
        Log.d("KEY_PRESSED - onKeyDown", "KEYCODE_DEL");
    }
    return super.onKeyDown(keyCode, event);
}

//To detect DELETE KEY PRESS event there is no text in EditText
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_DEL) {
        Log.d("KEY_PRESSED - onKeyUp", "KEYCODE_DEL");
    }
    return super.onKeyUp(keyCode, event);
}

NOTE: I am using Nexus 5X API 26 emulator

Upvotes: 4

Related Questions