71

I have to use TextInputLayout of design support library in my project. I want to give space between hint and EditText in TextInputLayout. I set margin and padding in TextInputLayout and even inside EditText but both are not work.So how to solve this issue. Here i attach screen shot and my coding.

==============================Style=================================

<style name="TextHint" parent="Base.TextAppearance.AppCompat">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">@color/green</item>
</style>



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

enter image description here

Pranav
  • 4,172
  • 3
  • 30
  • 31
  • 1
    why did you put the edit text in the android.support.design.widget.TextInputLayout – Coas Mckey Sep 16 '15 at 13:21
  • I guess he wanted to display a hint. But... doesn't the `hint` xml tag work? – Mauker Sep 16 '15 at 13:22
  • Because i want to use Floating hint for that EditText. – Pranav Sep 16 '15 at 13:22
  • @Mauker no hint tag work as you see in image hint display but its overlapping EditText border..so Is there any way to give vertical distance between Hint and EditText? – Pranav Sep 16 '15 at 13:25
  • @parnav please read my updated answer it contains how to show the erro which you may wana need too as in the case of in correct things etc – Coas Mckey Sep 16 '15 at 13:32

20 Answers20

134

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>
Radu Topor
  • 1,504
  • 1
  • 11
  • 12
  • 1
    this should be the correct answer. @ganesh2shiv answer can cut the edittext background – Bronx Nov 13 '15 at 14:55
  • 1
    When we add `android:top` we are creating an top insert which is space between the border line at the top and edittext top side. so border will be below edit text top side. that why we are using `android:paddingTop` to compensate for the donated space. This answer locks! – Edijae Crusar Apr 11 '16 at 14:04
  • 1
    For people who may have an error : remove `.xml` from the background identifier – StevenTB May 25 '16 at 09:49
  • Can't you do that without adding some background? – Yar Nov 17 '17 at 17:10
  • Thanks its working for me, I have just added padding to background box – Dildarkhan Pathan Dec 24 '19 at 13:34
  • 1
    Note that you must use x2 padding for your EditText to achieve default height and vertical centered input text. So if your drawable top padding is 16dp, your EditText padding must be 32dp – fHate Nov 26 '20 at 09:47
  • I need to show the floating label inside top of my input box. But it not works. Needs to show the label inside of the box. Any suggestions https://stackoverflow.com/questions/65367815/floating-label-needs-to-show-inside-the-textinput-layout-in-android-studio – sejn Jan 22 '21 at 10:09
  • perfect solution. working perfectly well for me. – Parmendra Singh Mar 10 '22 at 08:07
  • `` is what I did inside of the `` tags for a PNG image that I was using as a background. – Caleb Koch Feb 08 '23 at 17:46
34

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.

Community
  • 1
  • 1
Ganesh Mohan
  • 1,027
  • 10
  • 18
13

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.

axd
  • 626
  • 5
  • 13
12

@RaduTopor 's answer is good, but I think we also need add android:bottom or else it also de-centres the hint text displayed inside the EditText when not focused

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

Before(RaduTopor answer):

not centered

After:

centered

Chandler
  • 3,061
  • 3
  • 22
  • 30
6

After many attempts i found one more solution (material version 1.2.1):

this is layout sample:

  <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textInputLayout"
            style="@style/CustomTextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/textInputEditText"
                style="@style/CustomTextInputEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint" />

  </com.google.android.material.textfield.TextInputLayout>

this is styles sample:

 <style name="CustomTextInputLayout">
        <item name="boxCollapsedPaddingTop">-16dp</item>
        <item name="android:paddingTop">16dp</item>
        <item name="boxBackgroundColor">@color/backgroundColorWhite</item>
        <item name="android:textColorHint">...</item>
        <item name="android:textAppearance">...</item>
        <item name="hintTextAppearance">...</item>
        <item name="hintTextColor">...</item>
        <item name="boxStrokeColor">...</item>
    </style>

    <style name="CustomTextInputEditText">
        <item name="android:textAppearance">...</item>
        <item name="android:paddingTop">@dimen/margin_12</item>
        <item name="android:paddingBottom">@dimen/margin_12</item>
        <item name="android:paddingStart">0dp</item>
        <item name="android:paddingEnd">0dp</item>
    </style>

these 2 lines sets vertical offset for title/hint view in expanded state over collapsed state:

  <item name="boxCollapsedPaddingTop">-16dp</item>
  <item name="android:paddingTop">16dp</item>
 

these 2 lines - for margin between text and underline:

  <item name="android:paddingTop">@dimen/margin_12</item>
  <item name="android:paddingBottom">@dimen/margin_12</item>

that line - for removing backround tint bug:

 <item name="boxBackgroundColor">@color/backgroundColorWhite</item>

these 2 lines - for removing standard horizontal paddings

 <item name="android:paddingStart">0dp</item>
 <item name="android:paddingEnd">0dp</item>
Andrei K
  • 141
  • 2
  • 9
3

I made a special class for the purpose of adding empty view above EditText, thus add padding to hint. You can use it as simple TextInputLayout.

public class PaddingTextInputLayout extends TextInputLayout {

public View paddingView;

public PaddingTextInputLayout(Context context) {
    super(context);
}

public PaddingTextInputLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public PaddingTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
    super.addView(child, index, params);
    refreshPaddingView();
}

public void addPaddingView(){
    paddingView = new View(getContext());
    int height = (int) getContext().getResources()
            .getDimension(R.dimen.edittext_text_input_layout_padding);
    ViewGroup.LayoutParams paddingParams = new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            height);
    super.addView(paddingView, 0, paddingParams);
}

public void refreshPaddingView(){
    if (paddingView != null) {
        removeView(paddingView);
        paddingView = null;
    }
    addPaddingView();
}
}

This implementation is very simple and stupid, you can improve it a lot.

3

To disable the bottom line cut effect i have used margins, translationY and clipChildren="false"

<android.support.design.widget.TextInputLayout
        android:id="@+id/textinput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:clipChildren="false">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/et_profile_contact_name"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:background="@drawable/image_back"
            android:hint="Contact name"
            android:padding="5dp"
            android:translationY="3dp" />
    </android.support.design.widget.TextInputLayout>
Pavel Poley
  • 5,307
  • 4
  • 35
  • 66
2

This is what i did in my project for getting enough space between edittext and hint

<android.support.design.widget.TextInputLayout
    android:id="@+id/til_full_name"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_marginStart="40dp"
    android:layout_marginEnd="40dp"
    android:layout_marginTop="30dp"
    android:gravity="bottom"
    android:textColorHint="#fff"
    app:layout_constraintTop_toBottomOf="@+id/welcome_til_email">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#9a050505"
        android:hint="You email"
        android:inputType="textCapWords"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:singleLine="true"
        android:textAppearance="?android:textAppearanceSmall"
        android:textColor="#fff"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.design.widget.TextInputLayout>
Arjun
  • 358
  • 2
  • 13
2
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="10dp"
          android:bottom="5dp">
        <shape>
            <!-- Background Color -->
            <solid android:color="#f1f1f1"/>
            <!-- Round Corners -->
            <corners android:radius="5dp"/>
        </shape>
    </item>
</layer-list>

<style name="text_input_edit_text_without_border_style">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">55dp</item>
    <item name="android:paddingTop">10dp</item>
    <item name="android:paddingBottom">5dp</item>
    <item name="android:gravity">left|center_vertical</item>
    <item name="android:paddingLeft">8dp</item>
    <item name="android:paddingRight">8dp</item>
    <item name="android:maxLines">1</item>
    <item name="android:singleLine">true</item>
    <item name="android:imeOptions">actionNext</item>
    <item name="android:textSize">@dimen/text_size_large</item>
    <item name="android:background">@drawable/bg_without_border_with_padding_top</item>
</style>

<style name="text_input_layout_style">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginBottom">@dimen/margin_medium</item>
</style>
Dinesh Bagvan
  • 129
  • 1
  • 3
1

Just add to your EditText custom background

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
        <shape android:shape="rectangle">
            <padding
                android:top="4dp"/> // your padding value
        </shape>
    </item>
// customize your background items if you need
</layer-list>
<android.support.design.widget.TextInputLayout
 ...>
    <android.support.design.widget.TextInputEditText
        ...
        android:background="@style/your_custom_background"/>

then apply it to your EditText and increase height of your EditText with your padding value if you use fix height

1

This a late answer but hope it helps, And I thinks its a bit better solution. Rather then giving background to your editText , Give that background to your TextInputLayout . And set edittext layout to transparent.

<android.support.design.widget.TextInputLayout
                android:id="@+id/tinput_phone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="05dp"
                android:background="@drawable/send_email_screen_element_bg"
                android:padding="05dp"
                android:theme="@style/grey_control_style">

                <android.support.v7.widget.AppCompatEditText
                    android:id="@+id/edt_phone"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:ems="10"
                    android:hint="@string/phone_hint_str"
                    android:inputType="text"
                    android:paddingStart="10dp"
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp"
                    android:singleLine="true"
                    android:textColor="@color/black"
                    android:textSize="14sp" />
            </android.support.design.widget.TextInputLayout>
Nouman Ghaffar
  • 3,780
  • 1
  • 29
  • 37
1

just use padding-bottom

<com.google.android.material.textfield.TextInputLayout



             app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                >

                <com.google.android.material.textfield.TextInputEditText

                   ....
                    android:paddingBottom="@dimen/dp_25"
...
                   />
            </com.google.android.material.textfield.TextInputLayout>
Abhay Pratap
  • 1,886
  • 11
  • 15
0

If the requirement is to add space between cursor and hint then you can try

editTextComment.setHint("  "+getString(R.string.add_a_comment));
ashishdhiman2007
  • 807
  • 2
  • 13
  • 28
0

layout xml

<com.google.android.material.textfield.TextInputLayout
    style="@style/TextInputLayout"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:theme="@style/TextInputLayoutHint">

    <androidx.appcompat.widget.AppCompatEditText
        style="@style/TextInputEditText"
        android:hint="Email ID"
        android:inputType="textEmailAddress"
        android:maxLines="1" />

</com.google.android.material.textfield.TextInputLayout>

styles.xml

<style name="TextInputLayout">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginStart">15dp</item>
    <item name="android:layout_marginLeft">15dp</item>
    <item name="android:layout_marginTop">10dp</item>
    <item name="android:layout_marginEnd">15dp</item>
    <item name="android:layout_marginRight">15dp</item>
</style>

<style name="TextInputLayoutHint" parent="">
    <item name="android:textColorHint">@color/colorHintNormal</item>
    <item name="colorControlActivated">@color/colorOrange</item>
    <item name="android:textSize">11dp</item>
</style>

<style name="TextInputEditText" parent="">
    <item name="android:translationY">10dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:background">@null</item>
    <item name="android:paddingBottom">15dp</item>
    <item name="android:textColor">@color/colorBlack</item>
    <item name="android:textColorHint">@color/colorHintActive</item>
    <item name="android:textSize">13sp</item>
</style>    
Ketan Ramani
  • 4,874
  • 37
  • 42
0

You can use startIconDrawable to TextInputLayout and make sure you set some dummy transparant icon(icon width would be the padding start value). so that you will get padding from start. Note: this is a hack, not a direct solution

0

If you want to set the android:hint padding to 0 on focus, add app:boxCollapsedPaddingTop="0dp" to your TextInputLayout.

Erkan
  • 140
  • 2
  • 11
0

You can try this solution and editTextStyle in TextInputLayout style only. For example:

<com.google.android.material.textfield.TextInputLayout
    ...
    android:layout_width="@dimen/element_width"
    android:layout_height="@dimen/element_height"
    ...
    style="@style/EditTextField.Container"
    ...
    android:hint="@string/you_hint_resource">

    <com.google.android.material.textfield.TextInputEditText
        ...
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        ... />

</com.google.android.material.textfield.TextInputLayout>

In your style section

<style name="EditTextField"/>

<style name="EditTextField.Container" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
    <item name="android:textColorHint">@color/edit_text_hint_color</item>
    <item name="hintTextColor">@color/edit_text_hint_color</item>
    <item name="hintTextAppearance">@style/PasswordInput.HintTextAppearance</item>
    <item name="materialThemeOverlay">@style/PasswordInput.MaterialThemeOverlayStyle</item>
</style>

<style name="EditTextField.HintTextAppearance">
    <item name="android:paddingBottom">20dp</item>
    <item name="android:paddingTop">10dp</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="EditTextField.MaterialThemeOverlayStyle">
    <item name="editTextStyle">@style/EditTextStyle</item>
</style>

<style name="EditTextStyle" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox.Dense">
    <item name="android:background">@drawable/edittext_body_container</item>
    <item name="android:paddingBottom">@dimen/input_text_bottom_padding</item>
    <item name="android:paddingStart">@dimen/input_text_padding_start</item>
    <item name="android:paddingTop">@dimen/input_text_top_padding</item>
    <item name="android:textCursorDrawable">@drawable/cursor_white_drawable</item>
    <item name="android:textSize">16sp</item>
    <item name="lineHeight">20sp</item>
</style>
Gora
  • 1
  • 1
0

Working Solution:

You have to give custom height to TextInputEditText and make a custom drawable then you will get your solution, HOPE IT WILL WORKS 100%

1st step:

 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/tilStart"
    android:layout_width="match_parent"
    android:textColorHint="@color/darkgreen"
    android:layout_marginTop="@dimen/_20sdp"
    android:layout_height="wrap_content"
    app:errorEnabled="true"
    android:layout_below="@+id/labelDescription">

  <com.google.android.material.textfield.TextInputEditText
      android:id="@+id/tietStart"
      android:layout_width="match_parent"
      android:layout_height="@dimen/_30sdp"
      android:hint="@string/start_offset"
      android:fontFamily="@font/gilroysemibold"
      android:background="@drawable/customedittext"
      android:text="@string/zero"
      android:gravity="bottom"
      android:paddingLeft="@dimen/_10sdp"
      android:textColor="@color/darkgreen"
      android:inputType="number"
      android:typeface="monospace"
      android:digits="0123456789."
      android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>

2nd step:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="@dimen/_10sdp">
    <shape >
        <solid android:color="@color/activitycolor"/>
        <corners android:radius="@dimen/_10sdp"/>
    </shape>
</item>
</layer-list>
0

Try to use paddingStart instead of padding left or right. It worked for me.

android:paddingStart="8dp"
-1

I think you should consider this:

The design library takes an interesting approach to customizing the EditText: it doesn’t change it directly. Instead, the TextInputLayout is used to wrap the EditText and provide the enhancements.

The first one, displaying a floating label when the user types something into the field, is done automagically. The TextInputLayout finds the EditText among its children and attaches itself as a TextWatcher, so it’s able to determine when the field has been modified and animates the movement of the hint from its regular place in the EditText to the floating label position above it.

The second enhancement, displaying the error message, requires a slight change in code. Instead of setting the error on the EditText, the error should be set on the TextInputLayout. That’s because there is no automatic way for the TextInputLayout to be notified when the error is set on the EditText. Here’s what the layout might look like:

<android.support.design.widget.TextInputLayout
    android:id="@+id/username_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/username_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/username_hint"/>
</android.support.design.widget.TextInputLayout>

Note that both the EditText and TextInputLayout need layout IDs. In the fragment, we would need to configure the TextInputLayout to enable displaying errors:

TextInputLayout usernameTextInputLayout = (TextInputLayout) view.findViewById(R.id.username_text_input_layout);
usernameTextInputLayout.setErrorEnabled(true);
...
usernameTextInputLayout.setError(R.string.username_required);
Mauker
  • 11,237
  • 7
  • 58
  • 76
Coas Mckey
  • 701
  • 1
  • 13
  • 39
  • let the hint appear in the Edit text – Coas Mckey Sep 16 '15 at 13:29
  • No that not what i want,I use background to edit text its expand edittext border beyond its default limit so that hint overlapping the edittext. Is there any method or xml tag that can make margin between Hint and Edittext upper border? – Pranav Sep 16 '15 at 13:33
  • android:paddingTop="20dp" or what ever suit you – Coas Mckey Sep 16 '15 at 13:37
  • and remove this tag android:layout_marginTop="10dp" in your TextinputLayout – Coas Mckey Sep 16 '15 at 13:38
  • 1
    Not working android:paddingTop="20dp" also because its make space inside EditText i want to make space Between Hint and Edittext... – Pranav Sep 16 '15 at 13:47
  • dud by setting paddingTop working for me , I have jsut tested , – Coas Mckey Sep 16 '15 at 14:05
  • just remove your background for a second and then test it – Coas Mckey Sep 16 '15 at 14:06
  • see this link , if we can reduce distance then we can also increase the padding , and As I tried giving padding top , really works for me, I think the problem is your background here is the link : http://stackoverflow.com/questions/30946041/how-do-you-reduce-space-between-floating-edittext-hint-and-edittext-box-in-andro – Coas Mckey Sep 16 '15 at 14:11
  • Background is mandatory when i use background to EditText then its default border limit is exceeds so i want to know is there any xml attribute or java method that can make vertical distance between hint and EditText. – Pranav Sep 16 '15 at 14:13
  • I was just saying you to make it confirm that your background creating problem , why dont you make your background smaller in height – Coas Mckey Sep 16 '15 at 14:28
  • I changed the height but that doesn't make any difference.Still issue remaining because image doesn't contain any transparent area in upper and bottom. – Pranav Sep 17 '15 at 06:53
  • can you please remove background and then try again – Coas Mckey Sep 17 '15 at 06:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89880/discussion-between-coas-mckey-and-pranav). – Coas Mckey Sep 17 '15 at 07:20