Skip to content

Commit

Permalink
[TextInputLayout] Apply tint when setting start icons
Browse files Browse the repository at this point in the history
If startIconTint is a plain color, refreshStartIconDrawableState() won't update the tint to the new drawable. To solve the issue and make the logic be consistent, calls applyStartIconTint() when a new icon is set.

Resolves #2141

PiperOrigin-RevId: 383632467
  • Loading branch information
drchen authored and veganafro committed Jul 8, 2021
1 parent 456f135 commit 4044183
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3107,6 +3107,7 @@ public void setStartIconDrawable(@DrawableRes int resId) {
public void setStartIconDrawable(@Nullable Drawable startIconDrawable) {
startIconView.setImageDrawable(startIconDrawable);
if (startIconDrawable != null) {
applyStartIconTint();
setStartIconVisible(true);
refreshStartIconDrawableState();
} else {
Expand Down Expand Up @@ -3461,7 +3462,10 @@ public void setEndIconDrawable(@DrawableRes int resId) {
*/
public void setEndIconDrawable(@Nullable Drawable endIconDrawable) {
endIconView.setImageDrawable(endIconDrawable);
refreshEndIconDrawableState();
if (endIconDrawable != null) {
applyEndIconTint();
refreshEndIconDrawableState();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -424,6 +425,26 @@ public void perform(UiController uiController, View view) {
};
}

public static ViewAction setStartIconTintMode(final PorterDuff.Mode tintMode) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextInputLayout.class);
}

@Override
public String getDescription() {
return "Set tint mode for the start icon";
}

@Override
public void perform(UiController uiController, View view) {
TextInputLayout layout = (TextInputLayout) view;
layout.setStartIconTintMode(tintMode);
}
};
}

public static ViewAction setStartIconOnClickListener(final OnClickListener onClickListener) {
return new ViewAction() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import static com.google.android.material.testutils.TextInputLayoutActions.setStartIconOnClickListener;
import static com.google.android.material.testutils.TextInputLayoutActions.setStartIconOnLongClickListener;
import static com.google.android.material.testutils.TextInputLayoutActions.setStartIconTintList;
import static com.google.android.material.testutils.TextInputLayoutActions.setStartIconTintMode;
import static com.google.android.material.testutils.TextInputLayoutActions.setSuffixText;
import static com.google.android.material.testutils.TextInputLayoutActions.setTransformationMethod;
import static com.google.android.material.testutils.TextInputLayoutMatchers.doesNotShowEndIcon;
Expand All @@ -53,6 +54,7 @@
import static com.google.android.material.testutils.TextInputLayoutMatchers.endIconIsChecked;
import static com.google.android.material.testutils.TextInputLayoutMatchers.endIconIsNotChecked;
import static com.google.android.material.testutils.TextInputLayoutMatchers.showsEndIcon;
import static com.google.common.truth.Truth.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
Expand All @@ -62,13 +64,17 @@
import static org.junit.Assert.assertTrue;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import androidx.core.graphics.drawable.TintAwareDrawable;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.KeyEvent;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.test.espresso.ViewAssertion;
import androidx.test.espresso.matcher.ViewMatchers.Visibility;
import androidx.test.filters.LargeTest;
Expand Down Expand Up @@ -525,6 +531,30 @@ public void testSetStartIconProgrammatically() {
assertEquals(contentDesc, textInputLayout.getStartIconContentDescription().toString());
}

@Test
public void testSetStartIconTint() {
final Activity activity = activityTestRule.getActivity();
final TextInputLayout textInputLayout = activity.findViewById(R.id.textinput_no_icon);
Drawable drawable = new TintCapturedDrawable();

// Set start icon
onView(withId(R.id.textinput_no_icon)).perform(
setStartIconTintList(ColorStateList.valueOf(Color.RED)));
onView(withId(R.id.textinput_no_icon)).perform(setStartIconTintMode(PorterDuff.Mode.MULTIPLY));
onView(withId(R.id.textinput_no_icon)).perform(setStartIcon(drawable));

// Assert the start icon's tint is set
assertNotNull(textInputLayout.getStartIconDrawable());
assertThat(textInputLayout.getStartIconDrawable()).isInstanceOf(TintCapturedDrawable.class);
assertEquals(
Color.RED,
((TintCapturedDrawable) textInputLayout.getStartIconDrawable())
.capturedTint.getDefaultColor());
assertEquals(
PorterDuff.Mode.MULTIPLY,
((TintCapturedDrawable) textInputLayout.getStartIconDrawable()).capturedTintMode);
}

@Test
public void testStartIconDisables() {
// Disable the start icon
Expand Down Expand Up @@ -876,4 +906,23 @@ private static ViewAssertion isPasswordToggledVisible(final boolean isToggledVis
}
};
}

private static class TintCapturedDrawable extends ColorDrawable implements TintAwareDrawable {
ColorStateList capturedTint;
PorterDuff.Mode capturedTintMode;

TintCapturedDrawable() {
super(Color.WHITE);
}

@Override
public void setTintList(@Nullable ColorStateList tint) {
capturedTint = tint;
}

@Override
public void setTintMode(@Nullable PorterDuff.Mode tintMode) {
capturedTintMode = tintMode;
}
}
}

0 comments on commit 4044183

Please sign in to comment.