ChristianCuevas
ChristianCuevas

Reputation: 2692

How to make Android EditText expand vertically when full

I have this EditText

<EditText
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:hint="@string/write_message"
    android:textColorHint="@color/primary_color"
    android:ems="10"
    android:imeOptions="actionDone"
    android:inputType="textImeMultiLine"
    android:id="@+id/message_input"
    android:layout_gravity="center_horizontal"
    android:backgroundTint="@color/primary_color"/>

When the box is filled up with user inputted text, it scrolls to the right to make room for more text, I do not like this behavior, and would prefer it if the EditText box expanded upwards when it needs more room? Is there a way to do this? Thanks.

Upvotes: 17

Views: 21062

Answers (3)

CoolMind
CoolMind

Reputation: 28748

In my case I have multiline text, but it showed one line and a keyboard:

enter image description here

Though I have already set android:inputType="textCapSentences|textAutoCorrect|textMultiLine", it didn't help. Then I understood that when the keyboard appears in DialogFragment, it collapses the EditText. See DialogFragment and force to show keyboard to show keyboard when DialogFragment shows.

Then I added a short delay (100-300 ms) before showing the keyboard. Now I have:

enter image description here

In AndroidManifest I set android:windowSoftInputMode="adjustResize" for current activity.

Upvotes: 0

Mardann
Mardann

Reputation: 2143

Use both flags: textMultiLine will wrap your input, and textImeMultiLine will provide a break-line key in your keyboard.

 <EditText
    ...
    android:layout_height="wrap_content"
    android:inputType="textImeMultiLine|textMultiLine"
    ... />

Upvotes: 2

matiash
matiash

Reputation: 55340

Yes, this actually involves two things:

  1. Making the EditText accept multi-line input.
  2. Having its height grow as more text lines are added.

Therefore, to achieve this, you need to set up:

android:inputType="textMultiLine"
android:layout_height="wrap_content"

(Be mindful of the difference between textMultiLine and textImeMultiLine).

The full XML snippet would be:

<EditText
    android:layout_width="match_parent"
    android:inputType="textMultiLine"
    android:layout_height="wrap_content"
    android:hint="@string/write_message"
    android:textColorHint="@color/primary_color"
    android:ems="10"
    android:imeOptions="actionDone"
    android:id="@+id/message_input"
    android:layout_gravity="center_horizontal"
    android:backgroundTint="@color/primary_color"/>    

Upvotes: 35

Related Questions