Hunt
Hunt

Reputation: 8435

new line when user press enter in edit text in 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;
            }
        });

Upvotes: 5

Views: 10801

Answers (3)

mihails.kuzmins
mihails.kuzmins

Reputation: 1440

Try this solution: https://stackoverflow.com/a/20475011/13179254 Basically what you need to do:

Kotlin:

isSingleLine = false

Java:

setSingleLine(false)

Xml:

android:singleLine="false"

Upvotes: 2

Akeshwar Jha
Akeshwar Jha

Reputation: 4576

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.

Upvotes: 11

gaurang
gaurang

Reputation: 2240

try this

<EditText
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:inputType="textMultiLine"
   android:ems="10"
   android:id="@+id/edt_address"
   android:hint="Shipping Address" />

hope it will help you

Upvotes: 2

Related Questions