Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: TextInputLayout won't center the hint

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.

like image 363
Jon Avatar asked Jul 30 '15 13:07

Jon


People also ask

How do you center text in TextInputLayout?

You can fix this by putting the TextInputLayout inside a FrameLayout with a fixed height and center it vertically. Finally you can animate all the thing. You simply need to use the TransitionManager of the support library when changing the padding. I hope this will solve your problem.

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.

How do I remove the default padding in TextInputLayout?

You can just set the start and end padding on the inner EditText to 0dp. Here's a screenshot with Show Layout Bounds turned on so you can see that the hints go all the way to the edge of the view. Save this answer.


1 Answers

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"
like image 198
Md Golam Rahman Tushar Avatar answered Sep 22 '22 10:09

Md Golam Rahman Tushar