Reputation: 8021
I have an edittext enclosed within a textinputlayout from the support library and I'm finding it impossible to make the hint appear in the center of the field. I've tried all the usual tricks to be found in other stackoverflow discussions, as you can see from the code sample, but the hint still stubbornly appears on the left of the edittext. How can I solve this?
Please don't suggest using paddingLeft or paddingStart to do this - I want something that's going to work cleanly across different devices so it has to be a straight solution rather than a workaround.
Just to be clear: If I remove the textinputlayout then the hint is properly centered. It's the textinputlayout that's causing the problem here.
<android.support.design.widget.TextInputLayout
android:id="@+id/someId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:ellipsize="start"
android:maxLines="1"
android:inputType="textPersonName"
>
<EditText
android:id="@+id/someId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="@string/somehint"
android:textColorHint="@color/somecolor"
android:textSize="20sp"
android:textColor="@android:color/black"
android:background="@android:color/white"
android:cursorVisible="true"
android:textCursorDrawable="@null"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:inputType="textPersonName"
android:maxLines="1"
android:ellipsize="start"
/>
</android.support.design.widget.TextInputLayout>
Using the latest version of the support library:
compile 'com.android.support:design:22.2.1'
Edit - things I've tried to resolve this:
1) replacing Edittext with android.support.v7.widget.AppCompatEditText - doesn't work
2) using app:hintTextAppearance="@style/TextAppearance.AppCompat" inside the textinputlayout - doesn't work
3) Instead of defining the hint in the edittext, setting the hint programmatically using textinputlayout.sethint inside the activity - doesn't work: the hint appears on the left.
4) Setting the hint on the edittext in onActivityCreated. This appeared to work initially, but I discovered that although the hint appears centered the functionality of inputtextlayout becomes broken and clicking on the edittext no longer performs the hint animation.
Upvotes: 9
Views: 6498
Reputation: 2375
To center the hint text in your EditText just add the following two attributes in your EditText. This will do the trick.
android:ellipsize="start"
android:gravity="center_horizontal"
Upvotes: 2
Reputation: 8021
There doesn't seem to be a way to do this (at least not that I can find). As a workaround I tried using the MaterialEdittext 3rd party library - also not ideal since although the small animated hint is centered, it appears inside the text box instead of outside it:
https://github.com/rengwuxian/MaterialEditText
Upvotes: 0