Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intentionally go to new line in an EditText?

I am creating an app which provides chat functionality in it. There is an EditText in the screen, on which I have set OnKeyListener. Whenever user presses the "Enter" key after typing the message, the message is posted on the screen. It works fine. Here is my EditText:

<EditText android:id="@+id/chatMessageField"
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:layout_gravity="center_horizontal|center_vertical"
    android:layout_marginRight="5dip" android:layout_marginLeft="5dip"
    android:maxLines="2" android:minLines="1" android:scrollbars="vertical"
    android:hint="@string/chatMessageHintText" android:textColorHint="#3b64a8">
</EditText>

The problem is when user wants to go to new line before the EditText wraps the text and goes to new line. Now if user wants to go to new line and presses "Enter" key, the message is sent.

In some chat messangers , I have seen that pressing "Shift+Enter"(or any other key-combination) simultaneously takes the user to new line. How can we detect "Shift+Enter"(or any other key-combination) keys pressed simultaneously in Android? Is there any way to achieve this functionality in Android?

like image 252
Yogesh Somani Avatar asked Nov 02 '12 06:11

Yogesh Somani


2 Answers

Just try for this... It may help.

Set the input of EditText as

youredittext.setInputType(EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE);

Then add:

android:singleLine="false"

or

youredittext.setSingleLine(false);

Also go Click Here

like image 185
Varun Vishnoi Avatar answered Oct 13 '22 07:10

Varun Vishnoi


In your OnKeyListener method, use check for Enter and Shift Enter and provide separate functionality of Send and New Line respectively.

like image 21
Chand51 Avatar answered Oct 13 '22 09:10

Chand51