I am trying to create edit text in which when user presses enter it goes to new line and allow user to enter more text.
here is my design part
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/discussion"
android:layout_width="match_parent"
android:layout_height="100dp"
android:inputType="textMultiLine"
android:maxLines="5"
android:hint="Discussion outcome"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "/>
</android.support.design.widget.TextInputLayout>
Here is my code, but the problem is cursor position is not going to the next line. i tried discussion.append("\n")
too but it didnt work
discussion= (EditText) findViewById(R.id.discussion);
discussion.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
Log.e(TAG , "Key Action "+keyEvent.getAction());
if(keyEvent.getAction() == KeyEvent.ACTION_UP && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER){
String data =discussion.getText().toString();
discussion.setText(data+"\n");
Log.e(TAG , "Key "+keyEvent.getKeyCode());
}
return false;
}
});
You should include the newline character \n
in the allowed digits in your xml:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/discussion"
android:layout_width="match_parent"
android:layout_height="100dp"
android:inputType="textMultiLine"
android:maxLines="5"
android:hint="Discussion outcome"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\n "/>
</android.support.design.widget.TextInputLayout>
Next, you don't need any key listeners if you do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With