jibs
jibs

Reputation: 3

How to change the background color of setError() in Edittext?

I am displaying a error message using setError() to EditText. I want to change the background color of setError() message which is black by default. I searched almost everywhere but i couldn't get a correct solution though there are questions asked on this.

Upvotes: 0

Views: 4495

Answers (2)

niceumang
niceumang

Reputation: 1431

you can change background color using java of setError() dynamically like this

EditText.setError("Your Error String"); EditText.setErrorColor(Color.parseColor("#000000"));

or EditText.setError("Your Error String"); EditText.setErrorColor(Color.BLUE);

Upvotes: 1

MONK
MONK

Reputation: 189

You have to wrap your EditText inside TextInputLayout(it comes with some cool animations out of the box) then you'll be able to set any color for the EditText.

xml:

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/inputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="36dp"
                app:passwordToggleEnabled="true"
                app:passwordToggleDrawable="@drawable/selector_password_toggle"
                app:passwordToggleTint="@android:color/black"
                app:errorTextAppearance="@style/errorText"
                app:hintTextAppearance="@style/hintText">

            <EditText
                    android:id="@+id/passwordInput"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/registration_hint_password"
                    android:inputType="textPassword"/>

        </com.google.android.material.textfield.TextInputLayout>

code in Kotlin:

        inputLayout.isErrorEnabled = true
        inputLayout.setErrorTextColor(ColorStateList.valueOf(Color.RED))
        inputLayout.error = ERROR MESSAGE

Upvotes: 0

Related Questions