2525import android .animation .ValueAnimator .AnimatorUpdateListener ;
2626import androidx .appcompat .content .res .AppCompatResources ;
2727import android .text .Editable ;
28- import android .text .TextUtils ;
2928import android .text .TextWatcher ;
3029import android .view .View ;
3130import 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