Vertical content align in Android EditText

AndroidLayoutAndroid EdittextAndroid XmlLayout Gravity

Android Problem Overview


I have a multi-line EditText:

<EditText 
    android:layout_gravity="center"
    android:id="@+id/txtMessage"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblMessage"
    android:layout_width="wrap_content"
    android:width="250dip"
    android:text=""
    android:maxLength="760"
    android:lines="4">
</EditText>

Now the content is aligned to the middle. And I'd like to have the content aligned to the top.

Is there a property to do that?

Android Solutions


Solution 1 - Android

Try this: android:gravity="top"

<EditText 
   android:layout_gravity="center"  
   android:id="@+id/txtMessage"
   android:layout_height="wrap_content" 
   android:layout_below="@+id/lblMessage"
   android:layout_width="wrap_content" 
   android:width="250dip"
   android:text="hello" 
   android:maxLength="760"
   android:gravity="top"                       
   android:lines="4"/>


so android:gravity="top" sets the text at the "Top" Position.

You can also set different android:gravity attribute value such as center, bottom, center_vertical, left, right, center_horizontal, etc.

Enjoy !!

Solution 2 - Android

Try

android:gravity="top"

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
QuestionBikeCuriousView Question on Stackoverflow
Solution 1 - AndroidParesh MayaniView Answer on Stackoverflow
Solution 2 - Androidalex.zherdevView Answer on Stackoverflow