Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 889
ChristianCuevas Avatar asked Dec 30 '14 00:12

ChristianCuevas


2 Answers

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"/>    
like image 195
matiash Avatar answered Nov 04 '22 18:11

matiash


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"
    ... />
like image 2
Mardann Avatar answered Nov 04 '22 19:11

Mardann