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

AndroidAndroid EdittextVertical AlignmentMultiline

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

Use android:gravity="top"

Solution 2 - Android

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

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

Solution 3 - Android

U can use this Edittext....This will help you.

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

Solution 4 - Android

Use this:

android:gravity="top"

or

android:gravity="top|left"

Solution 5 - Android

<EditText android:id="@+id/EditText02" android:layout_width="120dp"
	android:layout_height="wrap_content" android:lines="5" android:layout_centerInParent="true"
	android:gravity="top|left" android:inputType="textMultiLine"
	android:scrollHorizontally="false" android:minWidth="10.0dip"
	android:maxWidth="180dip" />

it will work

Solution 6 - Android

Now a day use of gravity start is best choise:

android:gravity="start"

For EditText (textarea):

<EditText
    android:id="@+id/EditText02"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:lines="5"
    android:gravity="start"
    android:inputType="textMultiLine"
/>

Solution 7 - Android

I think you can use layout:weight = 5 instead android:lines = 5 because when you port your app to smaller device - it does it nicely.. well, both attributes will accomplish your job..

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiond-manView Question on Stackoverflow
Solution 1 - AndroidCommonsWareView Answer on Stackoverflow
Solution 2 - AndroidNandagopal TView Answer on Stackoverflow
Solution 3 - AndroidDenny SharmaView Answer on Stackoverflow
Solution 4 - AndroidSamir MangroliyaView Answer on Stackoverflow
Solution 5 - AndroidAsad RaoView Answer on Stackoverflow
Solution 6 - AndroidSheikh HasibView Answer on Stackoverflow
Solution 7 - AndroidSanjay HerleView Answer on Stackoverflow