Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new line when user press enter in edit text in android

Tags:

android

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;
            }
        });
like image 321
Hunt Avatar asked Dec 15 '22 05:12

Hunt


1 Answers

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.

like image 196
Akeshwar Jha Avatar answered Dec 16 '22 19:12

Akeshwar Jha