235

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.

JJD
  • 44,755
  • 49
  • 183
  • 309
d-man
  • 53,999
  • 81
  • 200
  • 285

7 Answers7

377

Use android:gravity="top"

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
99

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" 
/>
Gibolt
  • 24,018
  • 9
  • 129
  • 89
Nandagopal T
  • 1,979
  • 8
  • 40
  • 54
  • 8
    Do not use `top|left` unless you know you can control all the languages in your app, there are a lot of right-to-left languages out there (http://en.wikipedia.org/wiki/Right-to-left) Keep it localization-friendly – MariusBudin Jan 17 '14 at 08:44
  • 1
    @MariusBudin Presumably, `top|start` would be the correct alternative? – ban-geoengineering Oct 12 '19 at 10:10
19

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" />
Denny Sharma
  • 2,855
  • 1
  • 11
  • 14
9

Use this:

android:gravity="top"

or

android:gravity="top|left"
Tim Cooper
  • 144,163
  • 35
  • 302
  • 261
Samir Mangroliya
  • 38,074
  • 16
  • 111
  • 131
6
<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

Asad Rao
  • 2,928
  • 1
  • 19
  • 23
3

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"
/>
Hasib Akter
  • 6,859
  • 2
  • 22
  • 34
  • Can you shed some light on why this works? I would have expected `start` to have worked like `left` (for Western languages) - i.e., only affect the horizontal gravity, but it also seems to do the job of `top`, too. – ban-geoengineering Oct 12 '19 at 10:20
  • Yes, you are right. We are mainly use this for support RTL. Like in Arabic language, it's go to Right, instead of Left. And It's also go to `top` that you already asked. In generally we start writing from Top-Left side, that's why this `START` also show same characteristics. @ban-geoengineering – Hasib Akter Oct 12 '19 at 11:24
2

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..

Sanjay Herle
  • 293
  • 2
  • 4
  • 13