Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android setOnEditorActionListener() doesn't fire

Tags:

java

android

ime

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?

like image 862
MainstreamDeveloper00 Avatar asked May 23 '13 07:05

MainstreamDeveloper00


1 Answers

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");
        }
    }
});
like image 134
Dmitry Guselnikov Avatar answered Sep 18 '22 06:09

Dmitry Guselnikov