1

How to align the error text message to the center and below of edittext.I tried to align but its not working.Here is my code:

enter image description here

Xml

<android.support.design.widget.TextInputLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/tilEmail">
 <EditText
                    android:layout_width="match_parent"
                    android:id="@+id/mob"
                    android:layout_height="35dp"
                    android:background="@drawable/round_corner_3"
                    android:inputType="phone"
                    android:hint="Mobile Number"
                    android:textColorHint="@color/colorGray"
                    android:textColor="@color/Black"
                        android:drawablePadding="10dp"/>
                </android.support.design.widget.TextInputLayout>

Java class

til = (TextInputLayout) findViewById(R.id.tilEmail);

                     til.setError("Mobile number not valid");

                     til.setError(Html.fromHtml("<font color='red'>Mobile number not valid</font>"));
                    mobile.setError(null);

         mobile.getBackground().setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
                    Toast.makeText(getApplicationContext(), "Phone number or Password is not valid", Toast.LENGTH_SHORT).show();

                }
pratival
  • 111
  • 8

2 Answers2

1
// reference an error textview
TextView textView = (TextView) til.findViewById(android.support.design.R.id
            .textinput_error);

if (textView != null) {
    // can be RelativeLayout/FrameLayout Params, depending on your xml
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
    params.gravity = Gravity.CENTER|Gravity.BOTTOM;
    textView.setLayoutParams(params);
}

Additionally, you can add margins to LayoutParams

Boris Kozyrev
  • 152
  • 1
  • 5
0

You need to create a custom class extending TextInputLayout and set the gravity there. This because you can't get the R.id.textinput_error_text id before having an error previous set.

Using Kotlin:

class TextInputLayoutErrorEnd(context: Context, attrs: AttributeSet) :
    TextInputLayout(context, attrs) {
    override fun setError(errorText: CharSequence?) {
        super.setError(errorText)
        errorText?.let {
            val errorTextView = findViewById<TextView>(R.id.textinput_error_text)
            errorTextView.gravity = Gravity.END
        }
    }
}

On your XML:

<package.TextInputLayoutErrorEnd...>
    <com.google.android.material.textfield.TextInputEditText.../>
</package.TextInputLayoutErrorEnd>
Dimas Mendes
  • 2,664
  • 1
  • 21
  • 19