14

I'm trying to set a listener to EditText when enter button will be pressed.But it didn't fire at all. I tested this on LG Nexus 4 with Android 4.2.2. setOnEditorActionListener works on Amazon Kindle Fire with Android 2.3 and setImeActionLabel works nowhere! I also can't set text for Enter button.Here is code:

mEditText.setImeActionLabel("Reply", EditorInfo.IME_ACTION_UNSPECIFIED);
mEditText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
            return true;
        }  
    });

What am I doing wrong? How can I fix this?

MainstreamDeveloper00
  • 8,436
  • 15
  • 56
  • 102

9 Answers9

11

You can use TextWatcher.

editText.addTextChangedListener(new TextWatcher() {
    
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
        
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
        
    @Override
    public void afterTextChanged(Editable s) {
        if (s.charAt(s.length() - 1) == '\n') {
              Log.d("TAG", "Enter was pressed");
        }
    }
});
Stypox
  • 963
  • 11
  • 18
Dmitry Guselnikov
  • 1,048
  • 1
  • 8
  • 21
  • Thanks.It works. But don't you know why .setImeActionLabel and setOnEditorActionListener doesn't work on Nexus 4 but work in other devices? – MainstreamDeveloper00 May 23 '13 at 08:30
  • 2
    Android fragmentation magic. For my Galaxy Nexus they also don't work – Dmitry Guselnikov May 23 '13 at 08:33
  • 2
    For me the imeOptions only worked if the you set singleLine="true" on the EditText. Seems weird and annoying that might be by design but that is what I found. – speedynomads Dec 19 '14 at 17:37
  • For me it doesn't work, even after adding singleLine="true" in ET attributes. The devices i noticed are Nexus 6, Nexus 4 and Moto G. the similarity in all is Android 5.0 ! So, if anyone comes across the same issue. provide an answer please. – sud007 Apr 21 '15 at 06:36
  • 1
    For Kotlin, the condition will be like so: `if(s[s.length - 1] == '\n')` – Lilia Nov 26 '19 at 12:22
8

Make sure that you have an IME_ACTION set in the layout file:

<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

For a full explanation, see http://developer.android.com/guide/topics/ui/controls/text.html.

BeccaP
  • 1,452
  • 1
  • 13
  • 19
4

What worked for me is this,I have added this below line to EditText

android:imeOptions="actionSend"

this line makes keyboard which pops up when clicked on edit text has send button instead of search

in the setOnEditorActionListener you override following method looking for action send

@Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        if (actionId == EditorInfo.IME_ACTION_SEND) {
        //implement stuff here
          }
        }
Abhilash Reddy
  • 428
  • 1
  • 4
  • 19
3

In the xml file add tag android:inputType="text" to the EditText

Nakul
  • 31
  • 3
3

I use the following code

<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/ic_magnify"
    android:layout_centerVertical="true"
    android:textSize="15sp"
    android:textColor="#000"
    android:id="@+id/input_search"
    android:inputType="text"
    android:background="@null"
    android:hint="Enter Address, City or Zip Code"
    android:imeOptions="actionSearch"/>

mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) {

                if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE
                        || keyEvent.getAction() == KeyEvent.ACTION_DOWN || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

                        geoLocate();

                }

                return false;
            }
        });
Dinesh
  • 169
  • 2
  • 8
0

You can try this to use the OnKeyListener

editText.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int actionId, KeyEvent event) {
            Log.d("TEST RESPONSE", "Action ID = " + actionId + "KeyEvent = " + event);
            return false;
        }
    });
vmathieu
  • 220
  • 2
  • 7
  • I've this in my logcat : Action ID = 66KeyEvent = KeyEvent { action=ACTION_UP, keyCode=KEYCODE_ENTER, scanCode=0, metaState=0, flags=0x6, repeatCount=0, eventTime=653776952, downTime=653776952, deviceId=-1, source=0x0 } and i'm using my code – vmathieu May 23 '13 at 08:02
  • 1
    Read the docs for View.OnKeyListener, "This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener." http://developer.android.com/reference/android/view/View.OnKeyListener.html – speedynomads Dec 22 '14 at 10:03
0

I got it, it works on Edittext in an Activity, but not in a Fragement.

<EditText
                android:id="@+id/mEditText"
                android:imeOptions="actionSearch"
                android:imeActionLabel="搜索"
                android:inputType="text"
                android:singleLine="true"
                android:textSize="18dp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:paddingLeft="3dp"
                android:paddingRight="3dp"
                android:textColor="@color/black"
                android:layout_marginTop="7dp"
                android:layout_marginBottom="7dp"
                android:shadowColor="#4b000000"
                android:shadowDy="1"
                android:shadowDx="1"
                android:shadowRadius="1"
                android:cursorVisible="true"
                android:textCursorDrawable="@null"
                android:gravity="center_vertical|left"
                android:background="@color/transparent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                tools:ignore="UnusedAttribute">
            <requestFocus/>
        </EditText>


mEditText.setImeActionLabel("搜索", EditorInfo.IME_ACTION_SEARCH);
    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                MToastUtil.show("搜索");
            }
            return false;
        }
    });
weiyin
  • 6,819
  • 4
  • 47
  • 58
Michael Mao
  • 433
  • 5
  • 9
0

make sure:

inputType = "text";

imeOptions = "actionSend";

Kai Wang
  • 3,303
  • 1
  • 31
  • 27
-1

After some little more searching - this was the solution for me (works also in a fragment):

Just remove the imeAction

Community
  • 1
  • 1
Itay Sued
  • 134
  • 3