TextInputLayout :How to give padding or margin to hint? TextInputLayout :How to give padding or margin to hint? android android

TextInputLayout :How to give padding or margin to hint?


The solution proposed by ganesh2shiv works for the most part, although I've found it also de-centres the hint text displayed inside the EditText when not focused.

A better trick is to set the desired paddingTop to the EditText but also embed the extra padding within the EditText's background. A fairly sane way to do this is to wrap your original background in a <layer-list> and set the <item android:top="..."> attribute to match the paddingTop of your EditText.

<android.support.design.widget.TextInputLayout  android:layout_width="match_parent"  android:layout_height="wrap_content">  <EditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:paddingTop="@dimen/floating_hint_margin"    android:background="@drawable/bg_edit_text" /></android.support.design.widget.TextInputLayout>

And the bg_edit_text.xml drawable file:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  <item android:top="@dimen/floating_hint_margin">    <your original background; can be <bitmap> or <shape> or whatever./>  </item></layer-list>


I have been looking for the solution to this question from last couple of days myself. After tearing my hairs out for hours I have finally found a simple workaround. What I have noticed is that if you have custom background set on EditText the android:paddingTop attribute simple doesn't work to alter the spacing b/w the hint text and edit text (I have really no idea why). So if you have set custom background to your EditText, you can use android:translationY attribute in the EditText.

So here's your solution:

<android.support.design.widget.TextInputLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginLeft="30dp"    android:layout_marginRight="30dp"    android:layout_marginTop="10dp"    app:hintTextAppearance="@style/TextHint">    <EditText        android:id="@+id/edttxtEmailAddress"        android:layout_width="match_parent"        android:layout_height="50dp"        android:background="@drawable/rounded_common"        android:hint="@string/enter_valid_email"        android:paddingLeft="20dp"        android:translationY="10dp"         android:singleLine="true"        android:textSize="20sp" /></android.support.design.widget.TextInputLayout>

Hope it helps. :)


Update: My sincerest apologies for the late update. I have been really busy lately. If you haven't realized yet let me tell you this answer is straight out ugly and wrong. In retrospect I think I was probably drunk when I wrote it. As mentioned by others it might cut the bottom region of the editText background but there's an ugly fix for it as well - you can set the height of the (parent) textInputLayout sightly bigger (how big? you are supposed to find it by trial and error, yuck!) than the (child) editText. I hope you all do realize that would be crazy so please don't do it. Check out the answer written by @Radu Topor, it's by far the best and clean solution to this question. Thanks.


I tried the padding changes, but it doesn't work well.

A simple workaround is to change the height of the TIL:

android:layout_height="wrap_content"

to (e.g.)

android:layout_height="50dp"

it might not give the same result on all screen sizes.

And add

android:gravity="bottom" 

to push your text down.