Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

like image 955
Code Droid Avatar asked Jul 03 '12 00:07

Code Droid


People also ask

How do you set different gravity for Textinputlayout hint and text in android?

To set a different gravity for the EditText , don't set any gravity attributes in the layout, then set the EditText 's gravity programmatically, in your code. That is, after setContentView() , use findViewById() to get the EditText , then call setGravity(Gravity. CENTER_HORIZONTAL) on it.

What is the function of setting autoText attribute in EditText?

android:autoText If set, specifies that this TextView has a textual input method and automatically corrects some common spelling errors.


2 Answers

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" /> 
like image 180
kyau Avatar answered Sep 24 '22 05:09

kyau


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.

like image 20
Oleg Novosad Avatar answered Sep 26 '22 05:09

Oleg Novosad