How do I center the hint text within an EditText in Android?

I need to center the Hint text within an EditText in Android. How do I do this?


Solution 1:

In order for centering of hint text to work with EditText you have to make sure android:ellipsize="start" is defined. I don't know why this makes it work, but it does.

Example pulled from personal code:

<EditText
    android:id="@+id/player2Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:ellipsize="start"
    android:gravity="center_horizontal"
    android:hint="@string/player2_name"
    android:inputType="textCapWords|textPersonName"
    android:singleLine="true" />

Solution 2:

Actually, android:gravity="center_horizontal" creates the centering effect you're looking for. Similarly, you can use android:gravity="start" to put hint text at the beginning of the EditText view, and so on.

Solution 3:

Use this xml attribute:

android:gravity="center"

Solution 4:

use attribute

android:gravity="center"