32

TextInputLayout seems to always have some extra padding at the top (no matter that all margins/paddings are set to 0):

enter image description here

The layout looks like:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.AppCompatEditText
            android:id="@+id/txt_amount"
            style="@style/EditTextStyle"
            android:hint="@string/hint_amount"
            android:inputType="numberDecimal"/>
    </android.support.design.widget.TextInputLayout>

How to remove this extra space?

Kaushik
  • 6,150
  • 5
  • 39
  • 54
0leg
  • 13,464
  • 16
  • 70
  • 94
  • 1
    Do you mean above the text, or above the `View` bounds? http://stackoverflow.com/a/40696708 – Mike M. Apr 21 '17 at 11:40
  • @MikeM. I mean above the view (space between `EditText` and top border of the `TextInputLayout`). – 0leg Apr 21 '17 at 13:09
  • 4
    Yeah, that's for the floating hint. If you're not going to use that, you can disable it with `hintEnabled="false"` in the layout XML, like is shown in my linked answer. Oh, I guess someone posted it here, too. – Mike M. Apr 21 '17 at 13:11
  • 1
    How to remove mysterious padding on the sides though? – Sevastyan Savanyuk May 04 '18 at 12:35

9 Answers9

70

You can remove extra space above AppCompatEditText by setting app:hintEnabled="false" to TextInputLayout but it won't display hint until you re-enable that.

For more info goto Android Developer site -TextInputLayout

Checkout below code

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintEnabled="false">

    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/txt_amount"
        style="@style/EditTextStyle"
        android:hint="@string/hint_amount"
        android:inputType="numberDecimal"/>
</android.support.design.widget.TextInputLayout>

Hope this helpfull..

@Rajesh

Kaushik
  • 6,150
  • 5
  • 39
  • 54
Rajesh Peram
  • 1,128
  • 8
  • 10
16

The accepted answer didn't work for me on version - 1.2.1 so I did some experiments and realized that the padding doesn't actually come from the TextInputLayout, rather it's from TextInputEditText. So first I removed the padding from that, but it looked uneven, so I set custom padding to TextInputEditText and it worked fine. Just add this line android:padding="16dp" to the TextInputEditText replacing 16dp with your desired value.

Ali Akber
  • 492
  • 5
  • 9
15

having a structure like this:

<com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/til_project"
                    style="@style/LoginTextInputLayoutStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    app:errorEnabled="false">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/totalPiecesProduct"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:padding="0dp"
                        android:textSize="13sp"
                        style="@style/TextInputEditTextStyle"/>

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

the keys are:

app:errorEnabled="false" in TextInputLayout

`android:padding="0dp"` in `TextInputEditText`

before:

enter image description here

after

enter image description here

Gerrard
  • 819
  • 9
  • 7
1

You can do it by overriding the default padding style of TextInputStyle, (Material version should be above 1.1.0)

 <style name="CustomInputLayoutPadding" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="boxBackgroundColor">@color/transparent</item>
    <item name="materialThemeOverlay">@style/CustomOverlayFilledPadding</item>
</style>

then

 <style name="CustomeOverlayFilledPadding">
    <item name="editTextStyle">@style/CustomTextInputEditPaddingStyle</item>
</style>

then

   <style name="CustomTextInputEditPaddingStyle" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox">
    <item name="android:paddingStart" ns2:ignore="NewApi">2dp</item>
    <item name="android:paddingEnd" ns2:ignore="NewApi">2dp</item>
    <item name="android:paddingLeft">0dp</item>
    <item name="android:paddingRight">2dp</item>
    <item name="android:paddingTop">28dp</item>
    <item name="android:paddingBottom">12dp</item>
</style>
Azamat Mahkamov
  • 912
  • 14
  • 18
1

I had done using set manual padding in TextInputEditText

android:paddingHorizontal="@dimen/_12sdp"
android:paddingVertical="@dimen/_10sdp"
Madhav
  • 317
  • 2
  • 12
1

to remove all paddings in your TextInputLayout, you can set app:boxCollapsedPaddingTop to 0dp in TextInputLayout and android:padding to 0dp in Edittext.

Ali Kashan
  • 11
  • 3
0

As of compileSdk 33 or Material library version v1.8.0, I added the attribute

app:boxBackgroundMode="filled"

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

Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52
-1

You can use this

  <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@android:color/white">

            <com.bugle.customviews.RobotoLightEditText

                android:id="@+id/edtName"
                style="@style/StyledTilEditText"
                android:hint="@string/name"
                android:imeOptions="actionNext"
                android:inputType="textCapWords" />

        </android.support.design.widget.TextInputLayout>
-3

Add this line in EditText

android:translationY="-10dp"

Ashish
  • 13
  • 5
  • 3
    This looks like a hack. Would try to avoid it if it is possible to set this to zero somewhere. – 0leg Apr 21 '17 at 13:10