By Alvin Alexander. Last updated: June 6, 2016
I just added an editable EditText
widget to an Android application, and I needed to make the EditText
a certain height, and also make the text in the EditText
scroll, in case the user added some really long text. I used the following XML in my Android layout to make this happen:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textMultiLine" android:ems="10" android:id="@+id/quoteTextArea" android:editable="true" android:enabled="true" android:minLines="6" android:maxLines="6" android:isScrollContainer="true" android:hint="Type your quote here" android:background="#fafafa" android:textIsSelectable="true" android:focusable="true" android:gravity="top" android:padding="24dp" style="@style/Base.TextAppearance.AppCompat.Large" />
For the stated purposes, these are the most important lines in that EditText
XML:
android:inputType="textMultiLine" android:editable="true" android:enabled="true" android:minLines="6" android:maxLines="6" android:isScrollContainer="true" android:focusable="true"
This displays an EditText
field that the user can type into, and if they type some text that’s really long, the EditText
widget will automatically scroll. (As a result, the EditText
field won’t keep getting larger, and hide the text under the keyboard -- and that’s a good thing.)