5

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?

Yogesh Somani
  • 2,624
  • 3
  • 21
  • 34

4 Answers4

5

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

Varun Vishnoi
  • 980
  • 1
  • 9
  • 32
4

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

Akshay
  • 2,506
  • 4
  • 34
  • 55
Chand51
  • 924
  • 6
  • 10
  • 1
    How will I check for Shift and Enter button clicked simultaneously? Any code snippet? – Yogesh Somani Nov 02 '12 at 06:28
  • `if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {   // your Send functionality return true;   } else if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER && keyCode == (KEYCODE_SHIFT_LEFT ||KEYCODE_SHIFT_RIGHT ) )) {  // / your New line functionality return true; } `
    Code is not compiled, may contain syntax error, presented for functionality. Instead of Shift, you can use LongPress for Enter as well to achieve 'new line' functionality'
    – Chand51 Nov 02 '12 at 06:47
  • For KeyEvent and long press event, refer to http://developer.android.com/reference/android/view/KeyEvent.html – Chand51 Nov 02 '12 at 06:56
1

While searching you solution I got this link. Try it it may solve your problem.

And if some other best way you got please post, this is very useful and good question..,.

Thanks..,.

EDIT......................

Also see this link for better using the custom shortcuts..,.

Community
  • 1
  • 1
MKB
  • 7,587
  • 9
  • 45
  • 71
0

Here you can set 'OnKeyListener' method. For each key you can provide the functionality as follows..

editText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
    switch(keyCode) {
        case KeyEvent.KEYCODE_0:
            //handle code for pressing 0
            break;
        case KeyEvent.KEYCODE_1
            //handles code for pressing 1
        default:
            break;
    }
}
});

Just try to search the code for shift and enter

It may Works. Have fun.

Varun Vishnoi
  • 980
  • 1
  • 9
  • 32