for the first time I\'m using the new Android\'s widget TextInputLayout, it\'s very nice but I\'m facing some problem using setError method
this is my xml
You just have to change the color back to whatever you want after setting the error to null. Something like:
yourEditText.setError(null);
yourEditText.getBackground().mutate().setColorFilter(
ContextCompat.getColor(getContext() , R.color.somecolor),
PorterDuff.Mode.SRC_ATOP);
I have a trick to solve this problem simply:
1,new a class extend android.support.design.widget.TextInputEditText ; 2,overrvide getBackground() method ,make it return null;
becasue the method updateEditTextBackground() in TextInputLayout will judge if editText's background drawable is null,and now always return null,result is editText's background will not be changed by error text color.
I ran into similar problem and found a simple solution for it. This problem occurs if we set a custom background drawable/color to the EditText
inside the TextInputLayout
. Solution to this would be to subclass the the TextInputLayout
and override the setError()
and drawableStateChanged()
methods and set our custom drawable/color as the EditText's
background again. For Example, I had a rounded corner drawable set for my EditText's
background, below is my subclass,
public class RoundedBorderedTextInputLayout extends TextInputLayout {
private Context context;
public RoundedBorderedTextInputLayout(Context context) {
super(context);
this.context = context;
}
public RoundedBorderedTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public RoundedBorderedTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
EditText editText = getEditText();
if(editText != null) {
editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.custom_rounded_edittext));
}
}
@Override
public void setError(@Nullable final CharSequence error) {
super.setError(error);
EditText editText = getEditText();
if(editText != null) {
editText.setBackground(ContextCompat.getDrawable(this.context, R.drawable.custom_rounded_edittext));
}
}
}
And then use your custom class in the xml,
<com.example.RoundedBorderedTextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</com.example.RoundedBorderedTextInputLayout>
Hope this helps. Happy Android coding :)
This was a bug in support:design:23.2.0
(and possibly older versions) it was reported as an issue here
and has been fixed in the 23.3.0
update
set the style of your textinput
<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:textColorHint">@color/colorPrimaryDark</item>
<item name="android:textSize">14sp</item>
<item name="colorAccent">@color/colorPrimaryDark</item>
<item name="colorControlNormal">@color/colorPrimaryDark</item>
<item name="colorControlActivated">@color/colorPrimaryDark</item>
</style>
Put below code where you setError(null)
txt.setError(null);
//for plain white backgorund
editText.setBackgroundColor(getResources().getColor(android.R.color.white));
//or if you want any other than
editText.setBackgroundResource(R.drawable.border);
where border is my xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<stroke android:width="2dp" android:color="#ff207d94" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<corners android:radius="5dp" />
<solid android:color="#ffffffff" />
</shape>
got solution for only setError in this link
i.e. i have used custom TextInputLayout
this is my custom TextInputLayout
<com.adminworksite.Design.NoChangingBackgroundTextInputLayout
android:id="@+id/city_til_of_job_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_placeholder_five"
android:layout_marginLeft="@dimen/left_right"
android:layout_marginRight="@dimen/left_right"
app:counterEnabled="true"
app:counterMaxLength="35">
<EditText
android:id="@+id/city_et_of_job_create"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_background"
android:inputType="text"
android:paddingLeft="@dimen/left_right"
android:paddingRight="@dimen/left_right" />
</com.adminworksite.Design.NoChangingBackgroundTextInputLayout>
but if i set length limit to TextInputLayout i.e.
app:counterEnabled="true"
app:counterMaxLength="35"
the color still shows when limit exceeds and also changes color to pink
so i build one solution for it used addTextChangedListener to that edit text and set background by code
code snippet
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() == 0) {
textInputLayout.setErrorEnabled(true);
textInputLayout.setError("Input should not be empty!");
} else {
textInputLayout.setErrorEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
setBackgroundToEt(editText);
}
});
private void setBackgroundToEt(EditText editText) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
editText.setBackground(getResources().getDrawable(R.drawable.edittext_background));
}
}
and inside validation method in the end, i have again used set background to my editText
code snippet:
private boolean validateInputs() {
//validation code...
setBackgroundToEt(editText);
}