Skip to content

[TextInputLayout] background color of the indicatorArea when error is displayed #1580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import androidx.core.widget.TextViewCompat;
import androidx.appcompat.widget.AppCompatTextView;
Expand All @@ -46,6 +48,7 @@
import androidx.annotation.StyleRes;
import com.google.android.material.animation.AnimationUtils;
import com.google.android.material.animation.AnimatorSetCompat;
import com.google.android.material.shape.MaterialShapeDrawable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
Expand Down Expand Up @@ -107,6 +110,7 @@ final class IndicatorViewController {
@Nullable private CharSequence errorViewContentDescription;
private int errorTextAppearance;
@Nullable private ColorStateList errorViewTextColor;
@Nullable private ColorStateList indicatorAreaBackgroundColorOnError;

private CharSequence helperText;
private boolean helperTextEnabled;
Expand Down Expand Up @@ -158,6 +162,7 @@ void showError(final CharSequence errorText) {
}
updateCaptionViewsVisibility(
captionDisplayed, captionToShow, shouldAnimateCaptionView(errorView, errorText));
tintIndicatorAreaBackground();
}

void hideError() {
Expand All @@ -175,6 +180,25 @@ void hideError() {
}
updateCaptionViewsVisibility(
captionDisplayed, captionToShow, shouldAnimateCaptionView(errorView, null));
removeIndicatorAreaBackground();
}

void tintIndicatorAreaBackground(){
if (indicatorAreaBackgroundColorOnError == null){
return;
}
Drawable indicatorAreaBackground = indicatorArea.getBackground();
if (indicatorAreaBackground == null){
indicatorAreaBackground = new MaterialShapeDrawable();
}
DrawableCompat.setTintList(indicatorAreaBackground, indicatorAreaBackgroundColorOnError);
ViewCompat.setBackground(indicatorArea,indicatorAreaBackground);
}

void removeIndicatorAreaBackground(){
if (indicatorArea.getBackground()!=null){
ViewCompat.setBackground(indicatorArea,null);
}
}

/**
Expand Down Expand Up @@ -562,6 +586,17 @@ void setErrorViewTextColor(@Nullable ColorStateList errorViewTextColor) {
}
}

void setIndicatorAreaBackgroundColorOnError(ColorStateList indicatorAreaBackgroundColorOnError){
this.indicatorAreaBackgroundColorOnError = indicatorAreaBackgroundColorOnError;
if (errorView != null && (errorIsDisplayed() || !TextUtils.isEmpty(errorText))) {
if (indicatorAreaBackgroundColorOnError !=null) {
tintIndicatorAreaBackground();
} else {
removeIndicatorAreaBackground();
}
}
}

void setErrorTextAppearance(@StyleRes int resId) {
this.errorTextAppearance = resId;
if (errorView != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,10 @@ public TextInputLayout(@NonNull Context context, @Nullable AttributeSet attrs, i

setEnabled(a.getBoolean(R.styleable.TextInputLayout_android_enabled, true));

if (a.hasValue(R.styleable.TextInputLayout_indicatorAreaBackgroundColorOnError)) {
setIndicatorAreaBackgroundColorOnError(a.getColorStateList(R.styleable.TextInputLayout_indicatorAreaBackgroundColorOnError));
}

a.recycle();

// For accessibility, consider TextInputLayout itself to be a simple container for an EditText,
Expand Down Expand Up @@ -1644,6 +1648,11 @@ public int getErrorCurrentTextColors() {
return indicatorViewController.getErrorViewCurrentTextColor();
}

/** Sets the background color used by the error message in all states. */
public void setIndicatorAreaBackgroundColorOnError(@Nullable ColorStateList indicatorAreaBackgroundColorOnError) {
indicatorViewController.setIndicatorAreaBackgroundColorOnError(indicatorAreaBackgroundColorOnError);
}

/**
* Sets the text color and size for the helper text from the specified TextAppearance resource.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<public name="errorIconDrawable" type="attr"/>
<public name="errorIconTint" type="attr"/>
<public name="errorIconTintMode" type="attr"/>
<public name="indicatorAreaBackgroundColorOnError" type="attr"/>
<public name="helperText" type="attr"/>
<public name="helperTextEnabled" type="attr"/>
<public name="helperTextTextAppearance" type="attr"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2018 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.38" android:color="?attr/colorOnError" android:state_enabled="false"/>
<item android:color="?attr/colorOnError"/>
</selector>
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<!-- [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] -->
<enum name="screen" value="15"/>
</attr>
<attr name="indicatorAreaBackgroundColorOnError" format="reference"/>

<!-- Whether the layout is laid out as if the character counter will be displayed. -->
<attr name="counterEnabled" format="boolean"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<item name="counterOverflowTextColor">@color/mtrl_error</item>
<item name="errorTextColor">@color/mtrl_error</item>
<item name="helperTextTextColor">@color/mtrl_indicator_text_color</item>
<item name="indicatorAreaBackgroundColorOnError">@color/mtrl_bg_indicatorarea_error</item>
<!-- The color of the label when it is collapsed and the text field is active -->
<item name="hintTextColor">?attr/colorPrimary</item>
<item name="placeholderTextColor">@color/mtrl_indicator_text_color</item>
Expand Down