Search code examples
androidonclickkeyonclicklistenerenter

How can I check if enter on my keyboard is clicked?


This Question is no duplicate.

I want to check if enter on my keyboard is clicked.

This code is the most helpful for me:

 edittext.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            Toast.makeText(getActivity(), "it works", Toast
                    .LENGTH_SHORT).show();
            return false;
        }
    });

But it only works with the keyboard of my pc and not on my phone. I don't know weather it's important, but I am using a Fragment.

Here is my XML:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    android:id="@+id/edittext"
    android:imeOptions="actionDone"
    android:padding="10dp"/>

I looked at ALL These acticles and I tried every code, but nothing worked.

EDIT

I read a comment and so I want to add:

I am talking About Software Keyboard.


Solution

  • Is dependence on android:imeOptions="" of your EditText. Example, android:imeOptions="actionDone":

    editText.setOnEditorActionListener(new OnEditorActionListener() {        
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_DONE){
                //do something
            }
        return false;
        }
    });
    

    UPD: Add android:inputType="text" to your EditText in xml file

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:id="@+id/edittext"
        android:inputType="text"
        android:imeOptions="actionDone"
        android:padding="10dp"/>