Android: Vertical alignment for multi line EditText (Text area)


Question

I want to have 5 lines for the height of the text area. I am using the following code.

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:singleLine="false"
    android:lines="5"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip" />

The text area looks fine, but the problem is that the cursor is blinking in the middle of the text field. I want it to blink at first line, at the first character of the text field.

1
228
8/16/2012 5:03:07 PM

Accepted Answer

Use android:gravity="top"

365
3/15/2010 1:01:24 PM

This is similar to CommonsWare answer but with a minor tweak: android:gravity="top|left". Complete code example:

<EditText
    android:id="@+id/EditText02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="5"
    android:gravity="top|left"
    android:inputType="textMultiLine"
    android:scrollHorizontally="false" 
/>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon