Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect newline in EditText

How can I detect when I press the return button on the on-screen keyboard which creates a newline in the EditText field.

I don't really care if I have to check for newline characters or the return key, but I want to send a message by pressing the return key on the keyboard.

I already tried a few different things, but I can't seem to get it working.

My EditText object is called chatInputET if you want to know.

like image 413
Jochem Kuijpers Avatar asked Feb 24 '14 00:02

Jochem Kuijpers


2 Answers

add listener to your input:

chatInputET.addTextChangedListener( new TextWatcher(){
  @Override
  public void onTextChanged( CharSequence txt, int start, int before, int count ) {
    if( -1 != txt.toString().indexOf("\n") ){
      doSendMsg();
    }
  }
} );
like image 184
injecteer Avatar answered Sep 20 '22 02:09

injecteer


chatInputET.addTextChangedListener(new TextWatcher() {
  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
    String string = s.toString();
    if (string.length() > 0 && string.charAt(string.length() - 1) == '\n') {
      // do stuff
    }
  }
});
like image 45
abraabra Avatar answered Sep 20 '22 02:09

abraabra