android:gravity - Move Text inside a EditText (center, top, bottom, right, left, and more)

How to Move Text inside a EditText

* This would be the same in a TextView.

android:gravity added in API Level 1.


1. Create an Android project, if you don't already have one.

2. Add an EditText to your activity. 

3. In the main.xml file, add the below lines to display all the variants of gravity. 
android:lines = 3 adds 3 lines to the EditText so the gravity effect can be seen. android:gravity is the only true line needed though.

  <EditText  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:text="This is a EditText without gravity. The text is centered vertically in the EditText, left-justified."
               android:lines = "3"
/>

 <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity top. The text is at the top of the EditText, left-justified."
      android:gravity = "top"
    android:lines = "3"
 />

 <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity bottom. The text is on the bottom of EditText, left justified."
  android:gravity = "bottom"
    android:lines = "3"
 />
 
 <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity left. The text is left-justified at the top of the EditText."
       android:gravity = "left"
    android:lines = "3"
 />

 <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity right. The text is right-justified at the top of the EditText."
       android:gravity = "right"
    android:lines = "3"
 />

 <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity center_vertical. The text is centered vertically, but left justified."
       android:gravity = "center_vertical"
    android:lines = "3"
 />
 
   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity fill_vertical."
       android:gravity = "fill_vertical"
    android:lines = "3"
 />
 
   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity center_horizontal. The text is centered horizontally at the top of the EditText."
       android:gravity = "center_horizontal"
    android:lines = "3"
 />
 
   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity fill_horizontal."
       android:gravity = "fill_horizontal"
    android:lines = "3"
 />

   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity center. The text will be in the center vertically, and horizontally in the EditText."
       android:gravity = "center"
    android:lines = "3"
 />
 
   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity fill."
       android:gravity = "fill"
    android:lines = "3"
 />

   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity clip_vertical."
       android:gravity = "clip_vertical"
    android:lines = "3"
 />

   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity start. The text will be at the top left of the EditText."
       android:gravity = "start"
    android:lines = "3"
 />

   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="This is gravity end. The text will be at the bottom right of the EditText."
       android:gravity = "end"
  android:lines = "3"

 />

4. Depending on your device, you may not be able to view all the EditText objects. You can either add only some of them, or add a scrollview to your main.xml file.

5. Compile and run!