Skip to content

Commit 8a4f42a

Browse files
drchenhunterstich
authored andcommitted
[TextField] Make clear text icon focusable
The current logic hides clear text icon whenever edit text loses its focus. Therefore, the clear text icon can never be focused. Changes the logic so it retains clear text icon if it has focus. Resolves #2222 PiperOrigin-RevId: 380802379
1 parent b0558dc commit 8a4f42a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.animation.ValueAnimator.AnimatorUpdateListener;
2626
import androidx.appcompat.content.res.AppCompatResources;
2727
import android.text.Editable;
28-
import android.text.TextUtils;
2928
import android.text.TextWatcher;
3029
import android.view.View;
3130
import android.view.View.OnClickListener;
@@ -56,26 +55,26 @@ public void afterTextChanged(@NonNull Editable s) {
5655
if (textInputLayout.getSuffixText() != null) {
5756
return;
5857
}
59-
animateIcon(textInputLayout.hasFocus() && hasText(s));
58+
animateIcon(shouldBeVisible());
6059
}
6160
};
6261
private final OnFocusChangeListener onFocusChangeListener =
6362
new OnFocusChangeListener() {
6463
@Override
6564
public void onFocusChange(View v, boolean hasFocus) {
66-
boolean hasText = !TextUtils.isEmpty(((EditText) v).getText());
67-
animateIcon(hasText && hasFocus);
65+
animateIcon(shouldBeVisible());
6866
}
6967
};
7068
private final OnEditTextAttachedListener clearTextOnEditTextAttachedListener =
7169
new OnEditTextAttachedListener() {
7270
@Override
7371
public void onEditTextAttached(@NonNull TextInputLayout textInputLayout) {
7472
EditText editText = textInputLayout.getEditText();
75-
textInputLayout.setEndIconVisible(editText.hasFocus() && hasText(editText.getText()));
73+
textInputLayout.setEndIconVisible(shouldBeVisible());
7674
// Make sure there's always only one clear text text watcher added
7775
textInputLayout.setEndIconCheckable(false);
7876
editText.setOnFocusChangeListener(onFocusChangeListener);
77+
endIconView.setOnFocusChangeListener(onFocusChangeListener);
7978
editText.removeTextChangedListener(clearTextEndIconTextWatcher);
8079
editText.addTextChangedListener(clearTextEndIconTextWatcher);
8180
}
@@ -97,6 +96,10 @@ public void run() {
9796
if (editText.getOnFocusChangeListener() == onFocusChangeListener) {
9897
editText.setOnFocusChangeListener(null);
9998
}
99+
if (endIconView.getOnFocusChangeListener() == onFocusChangeListener) {
100+
endIconView.setOnFocusChangeListener(null);
101+
}
102+
100103
}
101104
}
102105
};
@@ -210,7 +213,10 @@ public void onAnimationUpdate(@NonNull ValueAnimator animation) {
210213
return animator;
211214
}
212215

213-
private static boolean hasText(@NonNull Editable editable) {
214-
return editable.length() > 0;
216+
private boolean shouldBeVisible() {
217+
EditText editText = textInputLayout.getEditText();
218+
return editText != null
219+
&& (editText.hasFocus() || endIconView.hasFocus())
220+
&& editText.getText().length() > 0;
215221
}
216222
}

0 commit comments

Comments
 (0)