8

I am using TextInputLayout with AppCompatEditText having a custom xml shape based background for AppCompatEditText. Whenever i set some error , the error line starts from beginning of layout. Is there any way to give padding to that error line.

enter image description here

BST Kaal
  • 2,993
  • 6
  • 34
  • 52

3 Answers3

6

You can reference error TextView by using:

TextView textView = (TextView) inputLayout.findViewById(android.support.design.R.id
            .textinput_error);

Then you can get its layout params to work with.

Boris Kozyrev
  • 152
  • 1
  • 5
  • 1
    risky hack is risky. at the same time tho, I don't think the id would change anytime soon – Aba May 20 '18 at 11:03
  • Keep in mind that the error text view is created only when error is displayed, so you need to change params after you display an error in your TextInputLayout. – Almighty May 22 '19 at 20:11
2

I struggled to solve this and this is what worked for me:

Removing the padding on the parent of the parent of my error textview

Using this solution I could find the error textview (cause I needed to add an icon to it) but couldn´t get rid of the padding, so after a few tries I realized that TextInputLayout has 2 childs so removing the padding from the second child did the trick and just to be sure, I removed it also from the first one

Using Kotlin extensions I came up with a clean solution

/**
 * TextInputLayout
 */
fun TextInputLayout.removePadding() {
    for (i in 0 until this.childCount) {
        this.getChildAt(i).setPadding(0)
    }
}

You can do it also in java using the same logic.

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
0
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
    android:id="@+id/tx_in_username"
    android:errorEnabled="true"
    android:layout_marginLeft="@dimen/margin_LEFT_RIGHT"
    android:layout_marginRight="@dimen/margin_LEFT_RIGHT"
android:layout_height="wrap_content">
<EditText
    android:layout_width="match_parent"
    android:layout_marginTop="10dp"
    android:layout_height="36sp"
    android:hint="Username"
    />

Mallikarjuna
  • 874
  • 6
  • 17