Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Customizing the setError icon for EditText kotlin

I Am using TextInputLayout. I try this but the icon is not shown. Help me to visible errorIcon in TextInputLayout.
I want to set a setError method to my EditText, with a custom icon instead of the default Android icon. So I tried this:

Thanks in Advance

 <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tilFirstName"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/civProfileImage"
                android:layout_marginStart="@dimen/margin_twenty_four"
                android:layout_marginTop="40dp"
                android:layout_marginEnd="@dimen/margin_twenty_four"
                android:layout_marginBottom="@dimen/margin_sixteen"
                android:hint="@string/first_name"
                android:textColorHint="@color/text_input_layout_hint_color"
                android:theme="@style/TextInputLayoutStyle"
                app:boxCornerRadiusBottomEnd="5dp"
                app:boxCornerRadiusBottomStart="5dp"
                app:boxCornerRadiusTopEnd="5dp"
                app:boxCornerRadiusTopStart="5dp"
                app:boxStrokeColor="@color/text_input_layout_stroke_color"
                app:boxStrokeErrorColor="@color/colorRed"
                app:errorTextColor="@color/colorRed"
                app:hintEnabled="true">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/edtFirstlName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@#!$%"
                    android:enabled="false"
                    android:focusable="false"
                    android:fontFamily="@font/roboto_regular"
                    android:imeOptions="actionNext"
                    android:inputType="textPersonName"
                    android:textColor="@color/colorTextNormal"
                    android:textSize="@dimen/text_size_sixteen" />
            </com.google.android.material.textfield.TextInputLayout>
        val drawable = resources.getDrawable(R.drawable.ic_baseline_error) as Drawable
        drawable.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
        tilFirstName.error = getString("Error", Drawable)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use the setErrorIconDrawable method in the TextInputLayout:

   tilFirstName.error = "Error text!!";
   tilFirstName.setErrorIconDrawable(R.drawable.xxxx); 
  

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading