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

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

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

Use this xml attribute:

>android:gravity="center"

Solution 4 - Android

use attribute

android:gravity="center"

Solution 5 - Android

I used this code in every circumstances, and it works perfectly without using android:ellipsize="start" to make a hint in center.

  <EditText
    android:id="@+id/player2Name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:hint="robi"
    android:inputType="text" />

Solution 6 - Android

I think the answer should be :

android:textAlignment="center"

Solution 7 - Android

textAlignment worked for me.

textAlignment="center"

Solution 8 - Android

Unfortunately, neither answer helped me aligning hint, written in LTR language, while the layout orientation was RTL. I needed such layout in a kind of translation application to make overlay icons not interfere with RTL text. But this trick didn't work with hints while there was no any text yet (icons appeared above the hint) and I came to the following java solution to layout problem:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
. . .
        if (isRightToLeft()){
            EditText guess = (EditText) rootView.findViewById(android.R.id.text1);
            CharSequence hint = "\u200F" + guess.getHint();
            guess.setHint(hint);
        }
. . .
}

The trick was in right-to-left mark to prepend a left-to-right string. And it seems to work, but does anyone know a more elegant solution?

Solution 9 - Android

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:hint="ENTER PIN"
            android:inputType="numberPassword" />
    </android.support.design.widget.TextInputLayout>

Note: in the tag TextInputEditText,the property

> android:gravity="center"

is what makes the deal of aligning the text in the center including the hint text

Solution 10 - Android

use this: android:gravity="center"

Solution 11 - Android

I use this and worked for me

android:gravity="Left|center_vertical"

Solution 12 - Android

use android:textAlignment="center" in EditText , it work for me

Solution 13 - Android

This worked for me:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/nombreslayoutinput">
    <EditText
        android:id="@+id/nombreslayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/nombres"
        android:layout_centerInParent="true"
        android:gravity="center|center_vertical"
        android:ellipsize="start"
        android:inputType="textCapWords|textPersonName"

        />
</android.support.design.widget.TextInputLayout>

Solution 14 - Android

The correct answer is

android:gravity="center_horizontal

Solution 15 - Android

> hint gravity

android:textAlignment="center"

> text gravity

android:gravity="left"

Solution 16 - Android

My problem was that the EditText wasn't big enought to fit exactly inside its parent (FrameLayout in my case), so using just android:gravity="center" (center_vertical, horizontal, start, or whatever) would just fit it inside the EditText, and not in its parent, so I had to center the EditText inside its parent using:

android:layout_gravity="center"

pd: I used android:background="@android:color/transparent" to hide the ugly underline in the EditText hint

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
QuestionCode DroidView Question on Stackoverflow
Solution 1 - AndroidkyauView Answer on Stackoverflow
Solution 2 - AndroidOleg NovosadView Answer on Stackoverflow
Solution 3 - AndroidAnkitkumar MakwanaView Answer on Stackoverflow
Solution 4 - AndroidAmr AngryView Answer on Stackoverflow
Solution 5 - AndroidRobi Kumar TomarView Answer on Stackoverflow
Solution 6 - AndroidAbhishek MathurView Answer on Stackoverflow
Solution 7 - AndroidDrNachtschattenView Answer on Stackoverflow
Solution 8 - AndroidCool SoftView Answer on Stackoverflow
Solution 9 - AndroidIshimwe Aubain ConsolateurView Answer on Stackoverflow
Solution 10 - AndroidRaj BediView Answer on Stackoverflow
Solution 11 - AndroidAli KhakiView Answer on Stackoverflow
Solution 12 - AndroidPritesh VishwakarmaView Answer on Stackoverflow
Solution 13 - AndroidFederico DassattiView Answer on Stackoverflow
Solution 14 - AndroidRick boyView Answer on Stackoverflow
Solution 15 - AndroidAminSTView Answer on Stackoverflow
Solution 16 - AndroidTincho825View Answer on Stackoverflow