How to Handle Enter Key Using TextWatcher on Android

M Rijalul Kahfi picture M Rijalul Kahfi · Oct 14, 2012 · Viewed 8.3k times · Source

I am working on Android. Previously i used onKeyListener to handle specific action on key event.

However, this way seems not to solve my problem since almost all key would get disable once i have implemented that listener to my EditText. After reading some topics in SO, i know that i should use TextWatcher instead, but i'm still wondering how to handle ENTER key event inside because parameters provided there are only CharSequence, Editable, etc. I didn't find any keyCode parameters.

Answer

user2348959 picture user2348959 · May 4, 2013

try this

@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")) {
        //enter pressed
    }
}