Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing TextInputLayout extra top padding

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?

like image 635
0leg Avatar asked Apr 21 '17 11:04

0leg


People also ask

How do I remove TextInputLayout padding?

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.

How do I remove TextInputLayout error?

Now you can simply do input. setError(..) for new error and input. setErrorEnabled(false) to remove it.

What is a TextInputLayout in Android?

TextInputLayout is a view container that is used to add more features to an EditText. It acts as a wrapper for EditText and has some features like: Floating hint. Animation that can be disabled or enabled. Error labels that display error messages when an error occurs.


Video Answer


3 Answers

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

like image 146
Rajesh Peram Avatar answered Oct 21 '22 01:10

Rajesh Peram


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.

like image 37
Ali Akber Avatar answered Oct 21 '22 03:10

Ali Akber


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

like image 14
Gerrard Avatar answered Oct 21 '22 02:10

Gerrard