Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable padding on TextInputLayout?

There appears to be left padding automatically added when using a TextInputLayout to wrap an EditText as you can see in the screenshot below.

enter image description here

There is no padding added to the EditText in the layout XML, but when the view is rendered there appears to be left padding on the EditText. You can see this when comparing the TextView below the TextInputLayout.

How do I disable this left padding from being added?

Thank you!

like image 364
dazza5000 Avatar asked Dec 30 '16 22:12

dazza5000


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.

How do I change the bottom line color in TextInputLayout?

To change the bottom line color you have to use the attribute: boxStrokeColor . You can also apply these attributes in your layout: <com.


2 Answers

You can just set the start and end padding on the inner EditText to 0dp.

<com.google.android.material.textfield.TextInputLayout   android:layout_width="match_parent"   android:layout_height="wrap_content">    <com.google.android.material.textfield.TextInputEditText     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:paddingStart="0dp"     android:paddingEnd="0dp" />  </com.google.android.material.textfield.TextInputLayout> 

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.

EditTextNoPadding

like image 103
Andrew Orobator Avatar answered Sep 20 '22 14:09

Andrew Orobator


With the TextInputLayout included in the Material Components Library you can use a custom style to reduce the padding.

Just use something like:

<com.google.android.material.textfield.TextInputLayout        ....        android:hint="Hint text"               style="@style/My.TextInputLayout.FilledBox.Padding" > 

Then you can define a custom style for the EditText using the materialThemeOverlay attribute:

  <style name="My.TextInputLayout.FilledBox.Padding" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">     <item name="materialThemeOverlay">@style/MyThemeOverlayFilledPadding</item>   </style>    <style name="MyThemeOverlayFilledPadding">     <item name="editTextStyle">@style/MyTextInputEditText_filledBox_padding</item>   </style>    <style name="MyTextInputEditText_filledBox_padding" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox">     <!-- left and right padding -->     <item name="android:paddingStart" ns2:ignore="NewApi">2dp</item>     <item name="android:paddingEnd" ns2:ignore="NewApi">2dp</item>     <item name="android:paddingLeft">2dp</item>     <item name="android:paddingRight">2dp</item>          <!-- top and bottom padding -->     <item name="android:paddingTop">28dp</item>     <item name="android:paddingBottom">12dp</item>   </style> 

Here the final result:

enter image description hereenter image description here

Note: it requires at least the version 1.1.0 of the Material Components library.

like image 31
Gabriele Mariotti Avatar answered Sep 18 '22 14:09

Gabriele Mariotti