Skip to content

Commit 86b512a

Browse files
drchenleticiarossi
authored andcommitted
[CleanUp][TextField] Move end icon and content description logic to the layout class
PiperOrigin-RevId: 448036109
1 parent d4c2c95 commit 86b512a

File tree

7 files changed

+77
-52
lines changed

7 files changed

+77
-52
lines changed

lib/java/com/google/android/material/textfield/ClearTextEndIconDelegate.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import android.view.View.OnClickListener;
2727
import android.view.View.OnFocusChangeListener;
2828
import android.widget.EditText;
29-
import androidx.annotation.DrawableRes;
3029
import androidx.annotation.NonNull;
3130
import androidx.annotation.Nullable;
3231
import com.google.android.material.animation.AnimationUtils;
@@ -59,16 +58,12 @@ class ClearTextEndIconDelegate extends EndIconDelegate {
5958
private AnimatorSet iconInAnim;
6059
private ValueAnimator iconOutAnim;
6160

62-
ClearTextEndIconDelegate(@NonNull EndCompoundLayout endLayout, @DrawableRes int customEndIcon) {
63-
super(endLayout, customEndIcon);
61+
ClearTextEndIconDelegate(@NonNull EndCompoundLayout endLayout) {
62+
super(endLayout);
6463
}
6564

6665
@Override
6766
void setUp() {
68-
endLayout.setEndIconDrawable(
69-
customEndIcon == 0 ? R.drawable.mtrl_ic_cancel : customEndIcon);
70-
endLayout.setEndIconContentDescription(
71-
endLayout.getResources().getText(R.string.clear_text_end_icon_content_description));
7267
endLayout.setEndIconCheckable(false);
7368
initAnimators();
7469
}
@@ -82,6 +77,16 @@ void tearDown() {
8277
}
8378
}
8479

80+
@Override
81+
int getIconDrawableResId() {
82+
return R.drawable.mtrl_ic_cancel;
83+
}
84+
85+
@Override
86+
int getIconContentDescriptionResId() {
87+
return R.string.clear_text_end_icon_content_description;
88+
}
89+
8590
@Override
8691
void onSuffixVisibilityChanged(boolean visible) {
8792
if (endLayout.getSuffixText() == null) {

lib/java/com/google/android/material/textfield/CustomEndIconDelegate.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package com.google.android.material.textfield;
1818

19-
import androidx.annotation.DrawableRes;
2019
import androidx.annotation.NonNull;
2120

2221
/**
@@ -28,14 +27,12 @@
2827
* TextInputLayout#setEndIconMode(int)}.
2928
*/
3029
class CustomEndIconDelegate extends EndIconDelegate {
31-
32-
CustomEndIconDelegate(@NonNull EndCompoundLayout endLayout, @DrawableRes int customEndIcon) {
33-
super(endLayout, customEndIcon);
30+
CustomEndIconDelegate(@NonNull EndCompoundLayout endLayout) {
31+
super(endLayout);
3432
}
3533

3634
@Override
3735
void setUp() {
38-
endLayout.setEndIconDrawable(customEndIcon);
3936
endLayout.setEndIconOnLongClickListener(null);
4037
}
4138
}

lib/java/com/google/android/material/textfield/DropdownMenuEndIconDelegate.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import android.widget.EditText;
4242
import android.widget.Spinner;
4343
import androidx.annotation.ChecksSdkIntAtLeast;
44-
import androidx.annotation.DrawableRes;
4544
import androidx.annotation.NonNull;
4645
import androidx.annotation.Nullable;
4746
import androidx.core.view.ViewCompat;
@@ -78,22 +77,12 @@ class DropdownMenuEndIconDelegate extends EndIconDelegate {
7877
private ValueAnimator fadeOutAnim;
7978
private ValueAnimator fadeInAnim;
8079

81-
DropdownMenuEndIconDelegate(
82-
@NonNull EndCompoundLayout endLayout, @DrawableRes int customEndIcon) {
83-
super(endLayout, customEndIcon);
80+
DropdownMenuEndIconDelegate(@NonNull EndCompoundLayout endLayout) {
81+
super(endLayout);
8482
}
8583

8684
@Override
8785
void setUp() {
88-
// For lollipop+, the arrow icon changes orientation based on dropdown popup, otherwise it
89-
// always points down.
90-
int drawableResId =
91-
customEndIcon == 0
92-
? (IS_LOLLIPOP ? R.drawable.mtrl_dropdown_arrow : R.drawable.mtrl_ic_arrow_drop_down)
93-
: customEndIcon;
94-
endLayout.setEndIconDrawable(drawableResId);
95-
endLayout.setEndIconContentDescription(
96-
endLayout.getResources().getText(R.string.exposed_dropdown_menu_content_description));
9786
initAnimators();
9887
accessibilityManager =
9988
(AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
@@ -121,6 +110,18 @@ void tearDown() {
121110
}
122111
}
123112

113+
@Override
114+
int getIconDrawableResId() {
115+
// For lollipop+, the arrow icon changes orientation based on dropdown popup, otherwise it
116+
// always points down.
117+
return IS_LOLLIPOP ? R.drawable.mtrl_dropdown_arrow : R.drawable.mtrl_ic_arrow_drop_down;
118+
}
119+
120+
@Override
121+
int getIconContentDescriptionResId() {
122+
return R.string.exposed_dropdown_menu_content_description;
123+
}
124+
124125
@Override
125126
boolean shouldTintIconOnError() {
126127
return true;

lib/java/com/google/android/material/textfield/EndCompoundLayout.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ void setEndIconMode(@EndIconMode int endIconMode) {
330330
dispatchOnEndIconChanged(previousEndIconMode);
331331
setEndIconVisible(endIconMode != END_ICON_NONE);
332332
EndIconDelegate delegate = getEndIconDelegate();
333+
setEndIconDrawable(getIconResId(delegate));
334+
setEndIconContentDescription(delegate.getIconContentDescriptionResId());
333335
if (delegate.isBoxBackgroundModeSupported(textInputLayout.getBoxBackgroundMode())) {
334336
delegate.setUp();
335337
} else {
@@ -347,6 +349,11 @@ void setEndIconMode(@EndIconMode int endIconMode) {
347349
applyIconTint(textInputLayout, endIconView, endIconTintList, endIconTintMode);
348350
}
349351

352+
private int getIconResId(EndIconDelegate delegate) {
353+
int customIconResId = endIconDelegates.customEndIconDrawableId;
354+
return customIconResId == 0 ? delegate.getIconDrawableResId() : customIconResId;
355+
}
356+
350357
void setEndIconOnClickListener(@Nullable OnClickListener endIconOnClickListener) {
351358
setIconOnClickListener(endIconView, endIconOnClickListener, endIconOnLongClickListener);
352359
}
@@ -678,12 +685,12 @@ private static class EndIconDelegates {
678685
private final SparseArray<EndIconDelegate> delegates = new SparseArray<>();
679686

680687
private final EndCompoundLayout endLayout;
681-
private final int endIconDrawableId;
688+
private final int customEndIconDrawableId;
682689
private final int passwordIconDrawableId;
683690

684691
EndIconDelegates(EndCompoundLayout endLayout, TintTypedArray a) {
685692
this.endLayout = endLayout;
686-
endIconDrawableId = a.getResourceId(R.styleable.TextInputLayout_endIconDrawable, 0);
693+
customEndIconDrawableId = a.getResourceId(R.styleable.TextInputLayout_endIconDrawable, 0);
687694
passwordIconDrawableId =
688695
a.getResourceId(R.styleable.TextInputLayout_passwordToggleDrawable, 0);
689696
}
@@ -700,14 +707,13 @@ EndIconDelegate get(@EndIconMode int endIconMode) {
700707
private EndIconDelegate create(@EndIconMode int endIconMode) {
701708
switch (endIconMode) {
702709
case END_ICON_PASSWORD_TOGGLE:
703-
return new PasswordToggleEndIconDelegate(
704-
endLayout, endIconDrawableId == 0 ? passwordIconDrawableId : endIconDrawableId);
710+
return new PasswordToggleEndIconDelegate(endLayout, passwordIconDrawableId);
705711
case END_ICON_CLEAR_TEXT:
706-
return new ClearTextEndIconDelegate(endLayout, endIconDrawableId);
712+
return new ClearTextEndIconDelegate(endLayout);
707713
case END_ICON_DROPDOWN_MENU:
708-
return new DropdownMenuEndIconDelegate(endLayout, endIconDrawableId);
714+
return new DropdownMenuEndIconDelegate(endLayout);
709715
case END_ICON_CUSTOM:
710-
return new CustomEndIconDelegate(endLayout, endIconDrawableId);
716+
return new CustomEndIconDelegate(endLayout);
711717
case END_ICON_NONE:
712718
return new NoEndIconDelegate(endLayout);
713719
default:

lib/java/com/google/android/material/textfield/EndIconDelegate.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import androidx.annotation.DrawableRes;
2727
import androidx.annotation.NonNull;
2828
import androidx.annotation.Nullable;
29+
import androidx.annotation.StringRes;
2930
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
3031
import com.google.android.material.internal.CheckableImageButton;
3132
import com.google.android.material.textfield.TextInputLayout.BoxBackgroundMode;
@@ -43,23 +44,32 @@ abstract class EndIconDelegate {
4344
final Context context;
4445
final CheckableImageButton endIconView;
4546

46-
@DrawableRes
47-
final int customEndIcon;
48-
49-
EndIconDelegate(@NonNull EndCompoundLayout endLayout, @DrawableRes int customEndIcon) {
47+
EndIconDelegate(@NonNull EndCompoundLayout endLayout) {
5048
this.textInputLayout = endLayout.textInputLayout;
5149
this.endLayout = endLayout;
5250
this.context = endLayout.getContext();
5351
this.endIconView = endLayout.getEndIconView();
54-
this.customEndIcon = customEndIcon;
5552
}
5653

5754
/** Called when the associated end icon mode is set. */
58-
abstract void setUp();
55+
void setUp() {}
56+
;
5957

6058
/** Called when the associated end icon mode is unset. */
6159
void tearDown() {}
6260

61+
/** Returns the icon resource ID that should be used. */
62+
@DrawableRes
63+
int getIconDrawableResId() {
64+
return 0;
65+
}
66+
67+
/** Returns the string resource ID that should be used as the content description. */
68+
@StringRes
69+
int getIconContentDescriptionResId() {
70+
return 0;
71+
}
72+
6373
/**
6474
* Whether the end icon should be tinted with the error color when the {@link TextInputLayout} is
6575
* in error mode.
@@ -138,4 +148,3 @@ void onInitializeAccessibilityNodeInfo(View host, @NonNull AccessibilityNodeInfo
138148
*/
139149
void onPopulateAccessibilityEvent(View host, @NonNull AccessibilityEvent event) {}
140150
}
141-

lib/java/com/google/android/material/textfield/NoEndIconDelegate.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
*/
2424
class NoEndIconDelegate extends EndIconDelegate {
2525
NoEndIconDelegate(@NonNull EndCompoundLayout endLayout) {
26-
super(endLayout, 0);
27-
}
28-
29-
@Override
30-
void setUp() {
31-
endLayout.setEndIconDrawable(null);
32-
endLayout.setEndIconContentDescription(null);
26+
super(endLayout);
3327
}
3428
}

lib/java/com/google/android/material/textfield/PasswordToggleEndIconDelegate.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
import androidx.annotation.DrawableRes;
2626
import androidx.annotation.NonNull;
2727
import androidx.annotation.Nullable;
28+
import androidx.annotation.StringRes;
2829

2930
/** Default initialization of the password toggle end icon. */
3031
class PasswordToggleEndIconDelegate extends EndIconDelegate {
32+
private int iconResId = R.drawable.design_password_eye;
3133

3234
@Nullable
3335
private EditText editText;
@@ -52,16 +54,15 @@ class PasswordToggleEndIconDelegate extends EndIconDelegate {
5254
};
5355

5456
PasswordToggleEndIconDelegate(
55-
@NonNull EndCompoundLayout endLayout, @DrawableRes int customEndIcon) {
56-
super(endLayout, customEndIcon);
57+
@NonNull EndCompoundLayout endLayout, @DrawableRes int overrideIconResId) {
58+
super(endLayout);
59+
if (overrideIconResId != 0) {
60+
iconResId = overrideIconResId;
61+
}
5762
}
5863

5964
@Override
6065
void setUp() {
61-
endLayout.setEndIconDrawable(
62-
customEndIcon == 0 ? R.drawable.design_password_eye : customEndIcon);
63-
endLayout.setEndIconContentDescription(
64-
endLayout.getResources().getText(R.string.password_toggle_content_description));
6566
endLayout.setEndIconVisible(true);
6667
endLayout.setEndIconCheckable(true);
6768
if (isInputTypePassword(editText)) {
@@ -78,6 +79,18 @@ void tearDown() {
7879
}
7980
}
8081

82+
@Override
83+
@DrawableRes
84+
int getIconDrawableResId() {
85+
return iconResId;
86+
}
87+
88+
@Override
89+
@StringRes
90+
int getIconContentDescriptionResId() {
91+
return R.string.password_toggle_content_description;
92+
}
93+
8194
@Override
8295
OnClickListener getOnIconClickListener() {
8396
return onIconClickListener;

0 commit comments

Comments
 (0)