2

I have an EditText in my app and I want it to have two lines, to show ime button instead of enter key and to move too long text to next line (like in sms apps). For now i have something like this:

<AutoCompleteTextView
    android:id="@+id/name_field"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_weight="1"
    android:background="@null"
    android:freezesText="true"
    android:hint="@string/some_hint"
    android:imeOptions="actionNext"
    android:maxLength="100"
    android:nextFocusDown="@null"
    android:lines="2"
    android:ellipsize="end"
    android:inputType="textImeMultiLine"
    android:selectAllOnFocus="true"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

It has two first properties I mentioned, but i can't recall any option that allows me to reach third.

For example: if one line in my EditText has 10 chars, i want to display text "abc abcd abc abcdefghijk" like that:

abc abcd
abc abc...

EDIT: It seems problem is in android:inputType="textImeMultiLine". When i changed it to android:inputType="textMultiLine" all works fine, but... I have enter button instead of IME button, which i want to avoid.

Seblis
  • 339
  • 4
  • 17
  • could you be more clear in what you exactly want.. – Hariharan Aug 21 '13 at 11:26
  • If text can't be displayed in a single line (because text is too long), i want to show the rest of it in the second line. – Seblis Aug 21 '13 at 11:29
  • `android:lines` may be.. – CRUSADER Aug 21 '13 at 11:31
  • If you give wrap_content as the edittext's height then it will automatically adjust its height according to the length of the conent.. – Hariharan Aug 21 '13 at 11:32
  • In my post you can see that I already set `android:lines="2"`. Unfortunately text without carriage return is shown only in first line. – Seblis Aug 21 '13 at 11:33
  • Removing android:inputType="textImeMultiLine" worked while i check.You can try that too! – Hariharan Aug 21 '13 at 11:42
  • what button you want at the place of enter button?? – Avinash Kumar Pankaj Aug 21 '13 at 11:54
  • @Hari Indeed, but then i need to know how to show IME button for multiline text. – Seblis Aug 21 '13 at 11:57
  • @AvinashKumarPankaj i want to show 'next' button as I set in `android:imeOptions="actionNext"`. It works if I set `android:singleLine="true"`, but i want to have this button and 2 lines at once. – Seblis Aug 21 '13 at 11:59
  • @Sebastian Lisiewski Try the answer which i have posted. i have checked it. It showed both the next button in keyboard and also if text is large it showed it in two lines. – Hariharan Aug 21 '13 at 12:08

4 Answers4

1

Try this..Hope it will work!

<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:background="@null"
    android:freezesText="true"
    android:imeOptions="actionNext"
    android:maxLength="100"
    android:nextFocusDown="@null"
    android:lines="2"
    android:ellipsize="end"
    android:selectAllOnFocus="true"
    android:textColor="@android:color/black"
    android:textSize="16sp" />
Hariharan
  • 24,741
  • 6
  • 50
  • 54
1

add this line

android:inputType="textMultiLine"

saigopi.me
  • 14,011
  • 2
  • 83
  • 54
0

add this in your xml android:maxLines="2" instead of android:lines="2"

Vamshi
  • 1,495
  • 1
  • 15
  • 31
0

None of these Methods worked for me. After Googling for about an hour, I found this piece of code:

EditText editText = findViewById(R.id.editNote);
    editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
    editText.setImeActionLabel("DONE",EditorInfo.IME_ACTION_DONE);              //Set Return Carriage as "DONE"
    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            if (event == null) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    // Capture soft enters in a singleLine EditText that is the last EditText
                    // This one is useful for the new list case, when there are no existing ListItems
                    editText.clearFocus();
                    InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                }

                else if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    // Capture soft enters in other singleLine EditTexts
                } else if (actionId == EditorInfo.IME_ACTION_GO) {
                } else {
                    // Let the system handle all other null KeyEvents
                    return false;
                }
            }
            else if (actionId == EditorInfo.IME_NULL) {
                // Capture most soft enters in multi-line EditTexts and all hard enters;
                // They supply a zero actionId and a valid keyEvent rather than
                // a non-zero actionId and a null event like the previous cases.
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    // We capture the event when the key is first pressed.
                } else {
                    // We consume the event when the key is released.
                    return true;
                }
            }
            else {
                // We let the system handle it when the listener is triggered by something that
                // wasn't an enter.
                return false;
            }
            return true;
        }
    });

Keep your EditText like this:

<EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/editNote"
            android:hint="Start Typing..."
            android:inputType="textMultiLine"
            android:gravity="start" />

I don't remember where I got this code from so can't credit the author. I made the necessary changes according to my need.

Mohd Zaid
  • 659
  • 6
  • 14