Skip to content

Commit 896e432

Browse files
leticiarossidsn5ft
authored andcommitted
Refactoring TextInputLayout.On*Listeners and restricting EndIconMode annotation.
PiperOrigin-RevId: 260516099
1 parent 858fa6a commit 896e432

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void afterTextChanged(Editable s) {
6363
private final OnEditTextAttachedListener clearTextOnEditTextAttachedListener =
6464
new OnEditTextAttachedListener() {
6565
@Override
66-
public void onEditTextAttached(EditText editText) {
66+
public void onEditTextAttached(TextInputLayout textInputLayout) {
67+
EditText editText = textInputLayout.getEditText();
6768
textInputLayout.setEndIconVisible(hasText(editText.getText()));
6869
// Make sure there's always only one clear text text watcher added
6970
textInputLayout.setEndIconCheckable(false);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,16 @@ public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
117117
private final OnEditTextAttachedListener dropdownMenuOnEditTextAttachedListener =
118118
new OnEditTextAttachedListener() {
119119
@Override
120-
public void onEditTextAttached(EditText editText) {
121-
AutoCompleteTextView autoCompleteTextView = castAutoCompleteTextViewOrThrow(editText);
120+
public void onEditTextAttached(TextInputLayout textInputLayout) {
121+
AutoCompleteTextView autoCompleteTextView =
122+
castAutoCompleteTextViewOrThrow(textInputLayout.getEditText());
122123

123124
setPopupBackground(autoCompleteTextView);
124125
addRippleEffect(autoCompleteTextView);
125126
setUpDropdownShowHideBehavior(autoCompleteTextView);
126127
autoCompleteTextView.setThreshold(0);
127-
editText.removeTextChangedListener(exposedDropdownEndIconTextWatcher);
128-
editText.addTextChangedListener(exposedDropdownEndIconTextWatcher);
128+
autoCompleteTextView.removeTextChangedListener(exposedDropdownEndIconTextWatcher);
129+
autoCompleteTextView.addTextChangedListener(exposedDropdownEndIconTextWatcher);
129130
textInputLayout.setErrorIconDrawable(null);
130131
textInputLayout.setTextInputAccessibilityDelegate(accessibilityDelegate);
131132

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public void afterTextChanged(Editable s) {}
5050
private final OnEditTextAttachedListener onEditTextAttachedListener =
5151
new OnEditTextAttachedListener() {
5252
@Override
53-
public void onEditTextAttached(EditText editText) {
53+
public void onEditTextAttached(TextInputLayout textInputLayout) {
54+
EditText editText = textInputLayout.getEditText();
5455
textInputLayout.setEndIconVisible(true);
5556
endIconView.setChecked(!hasPasswordTransformation());
5657
// Make sure there's always only one password toggle text watcher added
@@ -61,7 +62,7 @@ public void onEditTextAttached(EditText editText) {
6162
private final OnEndIconChangedListener onEndIconChangedListener =
6263
new OnEndIconChangedListener() {
6364
@Override
64-
public void onEndIconChanged(int previousIcon) {
65+
public void onEndIconChanged(TextInputLayout textInputLayout, int previousIcon) {
6566
EditText editText = textInputLayout.getEditText();
6667
if (editText != null && previousIcon == TextInputLayout.END_ICON_PASSWORD_TOGGLE) {
6768
// If the end icon was the password toggle add it back the PasswordTransformation

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.android.material.R;
2020

21+
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
2122
import static com.google.android.material.internal.ThemeEnforcement.createThemedContext;
2223
import static com.google.android.material.textfield.IndicatorViewController.COUNTER_INDEX;
2324

@@ -43,6 +44,7 @@
4344
import androidx.annotation.IntDef;
4445
import androidx.annotation.NonNull;
4546
import androidx.annotation.Nullable;
47+
import androidx.annotation.RestrictTo;
4648
import androidx.annotation.StringRes;
4749
import androidx.annotation.StyleRes;
4850
import androidx.annotation.VisibleForTesting;
@@ -240,7 +242,12 @@ public class TextInputLayout extends LinearLayout {
240242
private boolean hasStartIconTintMode;
241243
private Drawable startIconDummyDrawable;
242244

243-
/** Values for the end icon mode. */
245+
/**
246+
* Values for the end icon mode.
247+
*
248+
* @hide
249+
*/
250+
@RestrictTo(LIBRARY_GROUP)
244251
@IntDef({
245252
END_ICON_CUSTOM,
246253
END_ICON_NONE,
@@ -315,9 +322,9 @@ public interface OnEditTextAttachedListener {
315322
* #addOnEditTextAttachedListener(OnEditTextAttachedListener)} if the edit text is already
316323
* present.
317324
*
318-
* @param editText the {@link EditText}
325+
* @param textInputLayout the {@link TextInputLayout}
319326
*/
320-
void onEditTextAttached(EditText editText);
327+
void onEditTextAttached(TextInputLayout textInputLayout);
321328
}
322329

323330
/**
@@ -330,9 +337,10 @@ public interface OnEndIconChangedListener {
330337
/**
331338
* Called when the end icon changes.
332339
*
340+
* @param textInputLayout the {@link TextInputLayout}
333341
* @param previousIcon the {@link EndIconMode} the view previously had set
334342
*/
335-
void onEndIconChanged(@EndIconMode int previousIcon);
343+
void onEndIconChanged(TextInputLayout textInputLayout, @EndIconMode int previousIcon);
336344
}
337345

338346
private final LinkedHashSet<OnEditTextAttachedListener> editTextAttachedListeners =
@@ -2554,7 +2562,7 @@ public void clearOnEndIconChangedListeners() {
25542562
public void addOnEditTextAttachedListener(OnEditTextAttachedListener listener) {
25552563
editTextAttachedListeners.add(listener);
25562564
if (editText != null) {
2557-
listener.onEditTextAttached(editText);
2565+
listener.onEditTextAttached(this);
25582566
}
25592567
}
25602568

@@ -2774,7 +2782,7 @@ private EndIconDelegate getEndIconDelegate() {
27742782

27752783
private void dispatchOnEditTextAttached() {
27762784
for (OnEditTextAttachedListener listener : editTextAttachedListeners) {
2777-
listener.onEditTextAttached(editText);
2785+
listener.onEditTextAttached(this);
27782786
}
27792787
}
27802788

@@ -2797,7 +2805,7 @@ private boolean hasEndIcon() {
27972805

27982806
private void dispatchOnEndIconChanged(@EndIconMode int previousIcon) {
27992807
for (OnEndIconChangedListener listener : endIconChangedListeners) {
2800-
listener.onEndIconChanged(previousIcon);
2808+
listener.onEndIconChanged(this, previousIcon);
28012809
}
28022810
}
28032811

0 commit comments

Comments
 (0)