/
EndCompoundLayout.java
718 lines (619 loc) · 25.9 KB
/
EndCompoundLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.textfield;
import com.google.android.material.R;
import static com.google.android.material.textfield.IconHelper.applyIconTint;
import static com.google.android.material.textfield.IconHelper.refreshIconDrawableState;
import static com.google.android.material.textfield.IconHelper.setCompatRippleBackgroundIfNeeded;
import static com.google.android.material.textfield.IconHelper.setIconOnClickListener;
import static com.google.android.material.textfield.IconHelper.setIconOnLongClickListener;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_CLEAR_TEXT;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_CUSTOM;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_DROPDOWN_MENU;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_NONE;
import static com.google.android.material.textfield.TextInputLayout.END_ICON_PASSWORD_TOGGLE;
import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.TintTypedArray;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.MarginLayoutParamsCompat;
import androidx.core.view.ViewCompat;
import androidx.core.widget.TextViewCompat;
import com.google.android.material.internal.CheckableImageButton;
import com.google.android.material.internal.TextWatcherAdapter;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.textfield.TextInputLayout.EndIconMode;
import com.google.android.material.textfield.TextInputLayout.OnEditTextAttachedListener;
import com.google.android.material.textfield.TextInputLayout.OnEndIconChangedListener;
import java.util.LinkedHashSet;
/**
* A compound layout that includes views that will be shown at the end of {@link TextInputLayout}
* and their relevant rendering and presenting logic.
*/
@SuppressLint("ViewConstructor")
class EndCompoundLayout extends LinearLayout {
final TextInputLayout textInputLayout;
@NonNull private final FrameLayout endIconFrame;
@NonNull private final CheckableImageButton errorIconView;
private ColorStateList errorIconTintList;
private PorterDuff.Mode errorIconTintMode;
private OnLongClickListener errorIconOnLongClickListener;
@NonNull private final CheckableImageButton endIconView;
private final EndIconDelegates endIconDelegates;
@EndIconMode private int endIconMode = END_ICON_NONE;
private final LinkedHashSet<OnEndIconChangedListener> endIconChangedListeners =
new LinkedHashSet<>();
private ColorStateList endIconTintList;
private PorterDuff.Mode endIconTintMode;
private OnLongClickListener endIconOnLongClickListener;
@Nullable private CharSequence suffixText;
@NonNull private final TextView suffixTextView;
private boolean hintExpanded;
private EditText editText;
private final TextWatcher editTextWatcher =
new TextWatcherAdapter() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
getEndIconDelegate().beforeEditTextChanged(s, start, count, after);
}
@Override
public void afterTextChanged(Editable s) {
getEndIconDelegate().afterEditTextChanged(s);
}
};
private final OnEditTextAttachedListener onEditTextAttachedListener =
new OnEditTextAttachedListener() {
@Override
public void onEditTextAttached(@NonNull TextInputLayout textInputLayout) {
if (editText == textInputLayout.getEditText()) {
return;
}
if (editText != null) {
editText.removeTextChangedListener(editTextWatcher);
if (editText.getOnFocusChangeListener()
== getEndIconDelegate().getOnEditTextFocusChangeListener()) {
editText.setOnFocusChangeListener(null);
}
}
editText = textInputLayout.getEditText();
if (editText != null) {
editText.addTextChangedListener(editTextWatcher);
}
getEndIconDelegate().onEditTextAttached(editText);
setOnFocusChangeListenersIfNeeded(getEndIconDelegate());
}
};
EndCompoundLayout(TextInputLayout textInputLayout, TintTypedArray a) {
super(textInputLayout.getContext());
this.textInputLayout = textInputLayout;
setVisibility(GONE);
setOrientation(HORIZONTAL);
setLayoutParams(
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT,
Gravity.END | Gravity.RIGHT));
endIconFrame = new FrameLayout(getContext());
endIconFrame.setVisibility(GONE);
endIconFrame.setLayoutParams(
new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
errorIconView = createIconView(this, layoutInflater, R.id.text_input_error_icon);
endIconView = createIconView(endIconFrame, layoutInflater, R.id.text_input_end_icon);
endIconDelegates = new EndIconDelegates(this, a);
suffixTextView = new AppCompatTextView(getContext());
initErrorIconView(a);
initEndIconView(a);
initSuffixTextView(a);
endIconFrame.addView(endIconView);
addView(suffixTextView);
addView(endIconFrame);
addView(errorIconView);
textInputLayout.addOnEditTextAttachedListener(onEditTextAttachedListener);
}
private CheckableImageButton createIconView(
ViewGroup root, LayoutInflater inflater, @IdRes int id) {
CheckableImageButton iconView =
(CheckableImageButton) inflater.inflate(
R.layout.design_text_input_end_icon, root, false);
iconView.setId(id);
setCompatRippleBackgroundIfNeeded(iconView);
if (MaterialResources.isFontScaleAtLeast1_3(getContext())) {
ViewGroup.MarginLayoutParams lp =
(ViewGroup.MarginLayoutParams) iconView.getLayoutParams();
MarginLayoutParamsCompat.setMarginStart(lp, 0);
}
return iconView;
}
private void initErrorIconView(TintTypedArray a) {
if (a.hasValue(R.styleable.TextInputLayout_errorIconTint)) {
errorIconTintList =
MaterialResources.getColorStateList(
getContext(), a, R.styleable.TextInputLayout_errorIconTint);
}
if (a.hasValue(R.styleable.TextInputLayout_errorIconTintMode)) {
errorIconTintMode =
ViewUtils.parseTintMode(
a.getInt(R.styleable.TextInputLayout_errorIconTintMode, -1), null);
}
if (a.hasValue(R.styleable.TextInputLayout_errorIconDrawable)) {
setErrorIconDrawable(a.getDrawable(R.styleable.TextInputLayout_errorIconDrawable));
}
errorIconView.setContentDescription(
getResources().getText(R.string.error_icon_content_description));
ViewCompat.setImportantForAccessibility(
errorIconView, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
errorIconView.setClickable(false);
errorIconView.setPressable(false);
errorIconView.setFocusable(false);
}
private void initEndIconView(TintTypedArray a) {
// Set up the end icon if any.
if (!a.hasValue(R.styleable.TextInputLayout_passwordToggleEnabled)) {
// Default tint for any end icon or value specified by user
if (a.hasValue(R.styleable.TextInputLayout_endIconTint)) {
endIconTintList =
MaterialResources.getColorStateList(
getContext(), a, R.styleable.TextInputLayout_endIconTint);
}
// Default tint mode for any end icon or value specified by user
if (a.hasValue(R.styleable.TextInputLayout_endIconTintMode)) {
endIconTintMode =
ViewUtils.parseTintMode(
a.getInt(R.styleable.TextInputLayout_endIconTintMode, -1), null);
}
}
if (a.hasValue(R.styleable.TextInputLayout_endIconMode)) {
// Specific defaults depending on which end icon mode is set
setEndIconMode(a.getInt(R.styleable.TextInputLayout_endIconMode, END_ICON_NONE));
if (a.hasValue(R.styleable.TextInputLayout_endIconContentDescription)) {
setEndIconContentDescription(
a.getText(R.styleable.TextInputLayout_endIconContentDescription));
}
setEndIconCheckable(a.getBoolean(R.styleable.TextInputLayout_endIconCheckable, true));
} else if (a.hasValue(R.styleable.TextInputLayout_passwordToggleEnabled)) {
// Support for deprecated attributes related to the password toggle end icon
if (a.hasValue(R.styleable.TextInputLayout_passwordToggleTint)) {
endIconTintList =
MaterialResources.getColorStateList(
getContext(), a, R.styleable.TextInputLayout_passwordToggleTint);
}
if (a.hasValue(R.styleable.TextInputLayout_passwordToggleTintMode)) {
endIconTintMode =
ViewUtils.parseTintMode(
a.getInt(R.styleable.TextInputLayout_passwordToggleTintMode, -1), null);
}
boolean passwordToggleEnabled =
a.getBoolean(R.styleable.TextInputLayout_passwordToggleEnabled, false);
setEndIconMode(passwordToggleEnabled ? END_ICON_PASSWORD_TOGGLE : END_ICON_NONE);
setEndIconContentDescription(
a.getText(R.styleable.TextInputLayout_passwordToggleContentDescription));
}
}
private void initSuffixTextView(TintTypedArray a) {
// Set up suffix view.
suffixTextView.setVisibility(GONE);
suffixTextView.setId(R.id.textinput_suffix_text);
suffixTextView.setLayoutParams(
new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
Gravity.BOTTOM));
ViewCompat.setAccessibilityLiveRegion(
suffixTextView, ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE);
setSuffixTextAppearance(
a.getResourceId(R.styleable.TextInputLayout_suffixTextAppearance, 0));
if (a.hasValue(R.styleable.TextInputLayout_suffixTextColor)) {
setSuffixTextColor(a.getColorStateList(R.styleable.TextInputLayout_suffixTextColor));
}
setSuffixText(a.getText(R.styleable.TextInputLayout_suffixText));
}
void setErrorIconDrawable(@DrawableRes int resId) {
setErrorIconDrawable(resId != 0 ? AppCompatResources.getDrawable(getContext(), resId) : null);
refreshErrorIconDrawableState();
}
void setErrorIconDrawable(@Nullable Drawable errorIconDrawable) {
errorIconView.setImageDrawable(errorIconDrawable);
updateErrorIconVisibility();
applyIconTint(textInputLayout, errorIconView, errorIconTintList, errorIconTintMode);
}
Drawable getErrorIconDrawable() {
return errorIconView.getDrawable();
}
void setErrorIconTintList(@Nullable ColorStateList errorIconTintList) {
if (this.errorIconTintList != errorIconTintList) {
this.errorIconTintList = errorIconTintList;
applyIconTint(textInputLayout, errorIconView, errorIconTintList, errorIconTintMode);
}
}
void setErrorIconTintMode(@Nullable PorterDuff.Mode errorIconTintMode) {
if (this.errorIconTintMode != errorIconTintMode) {
this.errorIconTintMode = errorIconTintMode;
applyIconTint(textInputLayout, errorIconView, errorIconTintList, this.errorIconTintMode);
}
}
void setErrorIconOnClickListener(@Nullable OnClickListener errorIconOnClickListener) {
setIconOnClickListener(errorIconView, errorIconOnClickListener, errorIconOnLongClickListener);
}
CheckableImageButton getEndIconView() {
return endIconView;
}
EndIconDelegate getEndIconDelegate() {
return endIconDelegates.get(endIconMode);
}
@EndIconMode
int getEndIconMode() {
return endIconMode;
}
void setEndIconMode(@EndIconMode int endIconMode) {
if (this.endIconMode == endIconMode) {
return;
}
getEndIconDelegate().tearDown();
int previousEndIconMode = this.endIconMode;
this.endIconMode = endIconMode;
dispatchOnEndIconChanged(previousEndIconMode);
setEndIconVisible(endIconMode != END_ICON_NONE);
EndIconDelegate delegate = getEndIconDelegate();
if (delegate.isBoxBackgroundModeSupported(textInputLayout.getBoxBackgroundMode())) {
delegate.setUp();
} else {
throw new IllegalStateException(
"The current box background mode "
+ textInputLayout.getBoxBackgroundMode()
+ " is not supported by the end icon mode "
+ endIconMode);
}
setEndIconOnClickListener(delegate.getOnIconClickListener());
if (editText != null) {
delegate.onEditTextAttached(editText);
setOnFocusChangeListenersIfNeeded(delegate);
}
applyIconTint(textInputLayout, endIconView, endIconTintList, endIconTintMode);
}
void setEndIconOnClickListener(@Nullable OnClickListener endIconOnClickListener) {
setIconOnClickListener(endIconView, endIconOnClickListener, endIconOnLongClickListener);
}
void setEndIconOnLongClickListener(
@Nullable OnLongClickListener endIconOnLongClickListener) {
this.endIconOnLongClickListener = endIconOnLongClickListener;
setIconOnLongClickListener(endIconView, endIconOnLongClickListener);
}
void setErrorIconOnLongClickListener(
@Nullable OnLongClickListener errorIconOnLongClickListener) {
this.errorIconOnLongClickListener = errorIconOnLongClickListener;
setIconOnLongClickListener(errorIconView, errorIconOnLongClickListener);
}
private void setOnFocusChangeListenersIfNeeded(EndIconDelegate delegate) {
if (editText == null) {
return;
}
if (delegate.getOnEditTextFocusChangeListener() != null) {
editText.setOnFocusChangeListener(delegate.getOnEditTextFocusChangeListener());
}
if (delegate.getOnIconViewFocusChangeListener() != null) {
endIconView.setOnFocusChangeListener(delegate.getOnIconViewFocusChangeListener());
}
}
void refreshErrorIconDrawableState() {
refreshIconDrawableState(textInputLayout, errorIconView, errorIconTintList);
}
void setEndIconVisible(boolean visible) {
if (isEndIconVisible() != visible) {
endIconView.setVisibility(visible ? View.VISIBLE : View.GONE);
updateEndLayoutVisibility();
updateSuffixTextViewPadding();
textInputLayout.updateDummyDrawables();
}
}
boolean isEndIconVisible() {
return endIconFrame.getVisibility() == VISIBLE && endIconView.getVisibility() == VISIBLE;
}
void setEndIconActivated(boolean endIconActivated) {
endIconView.setActivated(endIconActivated);
}
void refreshEndIconDrawableState() {
refreshIconDrawableState(textInputLayout, endIconView, endIconTintList);
}
void setEndIconCheckable(boolean endIconCheckable) {
endIconView.setCheckable(endIconCheckable);
}
boolean isEndIconCheckable() {
return endIconView.isCheckable();
}
boolean isEndIconChecked() {
return hasEndIcon() && endIconView.isChecked();
}
void checkEndIcon() {
endIconView.performClick();
// Skip animation
endIconView.jumpDrawablesToCurrentState();
}
void setEndIconDrawable(@DrawableRes int resId) {
setEndIconDrawable(resId != 0 ? AppCompatResources.getDrawable(getContext(), resId) : null);
}
void setEndIconDrawable(@Nullable Drawable endIconDrawable) {
endIconView.setImageDrawable(endIconDrawable);
if (endIconDrawable != null) {
applyIconTint(textInputLayout, endIconView, endIconTintList, endIconTintMode);
refreshEndIconDrawableState();
}
}
@Nullable
Drawable getEndIconDrawable() {
return endIconView.getDrawable();
}
void setEndIconContentDescription(@StringRes int resId) {
setEndIconContentDescription(resId != 0 ? getResources().getText(resId) : null);
}
void setEndIconContentDescription(@Nullable CharSequence endIconContentDescription) {
if (getEndIconContentDescription() != endIconContentDescription) {
endIconView.setContentDescription(endIconContentDescription);
}
}
@Nullable
CharSequence getEndIconContentDescription() {
return endIconView.getContentDescription();
}
void setEndIconTintList(@Nullable ColorStateList endIconTintList) {
if (this.endIconTintList != endIconTintList) {
this.endIconTintList = endIconTintList;
applyIconTint(textInputLayout, endIconView, this.endIconTintList, endIconTintMode);
}
}
void setEndIconTintMode(@Nullable PorterDuff.Mode endIconTintMode) {
if (this.endIconTintMode != endIconTintMode) {
this.endIconTintMode = endIconTintMode;
applyIconTint(textInputLayout, endIconView, endIconTintList, this.endIconTintMode);
}
}
void addOnEndIconChangedListener(@NonNull OnEndIconChangedListener listener) {
endIconChangedListeners.add(listener);
}
void removeOnEndIconChangedListener(@NonNull OnEndIconChangedListener listener) {
endIconChangedListeners.remove(listener);
}
void clearOnEndIconChangedListeners() {
endIconChangedListeners.clear();
}
boolean hasEndIcon() {
return endIconMode != END_ICON_NONE;
}
TextView getSuffixTextView() {
return suffixTextView;
}
void setSuffixText(@Nullable CharSequence suffixText) {
this.suffixText = TextUtils.isEmpty(suffixText) ? null : suffixText;
suffixTextView.setText(suffixText);
updateSuffixTextVisibility();
}
@Nullable
CharSequence getSuffixText() {
return suffixText;
}
void setSuffixTextAppearance(@StyleRes int suffixTextAppearance) {
TextViewCompat.setTextAppearance(suffixTextView, suffixTextAppearance);
}
void setSuffixTextColor(@NonNull ColorStateList suffixTextColor) {
suffixTextView.setTextColor(suffixTextColor);
}
@Nullable
ColorStateList getSuffixTextColor() {
return suffixTextView.getTextColors();
}
void setPasswordVisibilityToggleDrawable(@DrawableRes int resId) {
setPasswordVisibilityToggleDrawable(
resId != 0 ? AppCompatResources.getDrawable(getContext(), resId) : null);
}
void setPasswordVisibilityToggleDrawable(@Nullable Drawable icon) {
endIconView.setImageDrawable(icon);
}
void setPasswordVisibilityToggleContentDescription(@StringRes int resId) {
setPasswordVisibilityToggleContentDescription(
resId != 0 ? getResources().getText(resId) : null);
}
void setPasswordVisibilityToggleContentDescription(@Nullable CharSequence description) {
endIconView.setContentDescription(description);
}
@Nullable
Drawable getPasswordVisibilityToggleDrawable() {
return endIconView.getDrawable();
}
@Nullable
CharSequence getPasswordVisibilityToggleContentDescription() {
return endIconView.getContentDescription();
}
boolean isPasswordVisibilityToggleEnabled() {
return endIconMode == END_ICON_PASSWORD_TOGGLE;
}
void setPasswordVisibilityToggleEnabled(boolean enabled) {
if (enabled && endIconMode != END_ICON_PASSWORD_TOGGLE) {
// Set password toggle end icon if it's not already set
setEndIconMode(END_ICON_PASSWORD_TOGGLE);
} else if (!enabled) {
// Set end icon to null
setEndIconMode(END_ICON_NONE);
}
}
void setPasswordVisibilityToggleTintList(@Nullable ColorStateList tintList) {
endIconTintList = tintList;
applyIconTint(textInputLayout, endIconView, endIconTintList, endIconTintMode);
}
void setPasswordVisibilityToggleTintMode(@Nullable PorterDuff.Mode mode) {
endIconTintMode = mode;
applyIconTint(textInputLayout, endIconView, endIconTintList, endIconTintMode);
}
void togglePasswordVisibilityToggle(boolean shouldSkipAnimations) {
if (endIconMode == END_ICON_PASSWORD_TOGGLE) {
endIconView.performClick();
if (shouldSkipAnimations) {
endIconView.jumpDrawablesToCurrentState();
}
}
}
void onHintStateChanged(boolean hintExpanded) {
this.hintExpanded = hintExpanded;
updateSuffixTextVisibility();
}
void onTextInputBoxStateUpdated() {
updateErrorIconVisibility();
// Update icons tints
refreshErrorIconDrawableState();
refreshEndIconDrawableState();
if (getEndIconDelegate().shouldTintIconOnError()) {
tintEndIconOnError(textInputLayout.shouldShowError());
}
}
private void updateSuffixTextVisibility() {
int oldVisibility = suffixTextView.getVisibility();
int newVisibility = (suffixText != null && !hintExpanded) ? VISIBLE : GONE;
if (oldVisibility != newVisibility) {
getEndIconDelegate().onSuffixVisibilityChanged(/* visible= */ newVisibility == VISIBLE);
}
updateEndLayoutVisibility();
// Set visibility after updating end layout's visibility so screen readers correctly announce
// when suffix text appears.
suffixTextView.setVisibility(newVisibility);
textInputLayout.updateDummyDrawables();
}
void updateSuffixTextViewPadding() {
if (textInputLayout.editText == null) {
return;
}
int endPadding =
(isEndIconVisible() || isErrorIconVisible())
? 0 : ViewCompat.getPaddingEnd(textInputLayout.editText);
ViewCompat.setPaddingRelative(
suffixTextView,
getContext()
.getResources()
.getDimensionPixelSize(R.dimen.material_input_text_to_prefix_suffix_padding),
textInputLayout.editText.getPaddingTop(),
endPadding,
textInputLayout.editText.getPaddingBottom());
}
@Nullable
CheckableImageButton getCurrentEndIconView() {
if (isErrorIconVisible()) {
return errorIconView;
} else if (hasEndIcon() && isEndIconVisible()) {
return endIconView;
} else {
return null;
}
}
boolean isErrorIconVisible() {
return errorIconView.getVisibility() == VISIBLE;
}
private void updateErrorIconVisibility() {
boolean visible =
getErrorIconDrawable() != null
&& textInputLayout.isErrorEnabled()
&& textInputLayout.shouldShowError();
errorIconView.setVisibility(visible ? VISIBLE : GONE);
updateEndLayoutVisibility();
updateSuffixTextViewPadding();
if (!hasEndIcon()) {
textInputLayout.updateDummyDrawables();
}
}
private void updateEndLayoutVisibility() {
// Sync endIconFrame's visibility with the endIconView's.
endIconFrame.setVisibility(
(endIconView.getVisibility() == VISIBLE && !isErrorIconVisible()) ? VISIBLE : GONE);
int suffixTextVisibility = (suffixText != null && !hintExpanded) ? VISIBLE : GONE;
boolean shouldBeVisible =
isEndIconVisible() || isErrorIconVisible() || suffixTextVisibility == VISIBLE;
setVisibility(shouldBeVisible ? VISIBLE : GONE);
}
private void dispatchOnEndIconChanged(@EndIconMode int previousIcon) {
for (OnEndIconChangedListener listener : endIconChangedListeners) {
listener.onEndIconChanged(textInputLayout, previousIcon);
}
}
private void tintEndIconOnError(boolean tintEndIconOnError) {
if (tintEndIconOnError && getEndIconDrawable() != null) {
// Setting the tint here instead of calling setEndIconTintList() in order to preserve and
// restore the icon's original tint.
Drawable endIconDrawable = DrawableCompat.wrap(getEndIconDrawable()).mutate();
DrawableCompat.setTint(
endIconDrawable, textInputLayout.getErrorCurrentTextColors());
endIconView.setImageDrawable(endIconDrawable);
} else {
applyIconTint(textInputLayout, endIconView, endIconTintList, endIconTintMode);
}
}
private static class EndIconDelegates {
private final SparseArray<EndIconDelegate> delegates = new SparseArray<>();
private final EndCompoundLayout endLayout;
private final int endIconDrawableId;
private final int passwordIconDrawableId;
EndIconDelegates(EndCompoundLayout endLayout, TintTypedArray a) {
this.endLayout = endLayout;
endIconDrawableId = a.getResourceId(R.styleable.TextInputLayout_endIconDrawable, 0);
passwordIconDrawableId =
a.getResourceId(R.styleable.TextInputLayout_passwordToggleDrawable, 0);
}
EndIconDelegate get(@EndIconMode int endIconMode) {
EndIconDelegate delegate = delegates.get(endIconMode);
if (delegate == null) {
delegate = create(endIconMode);
delegates.append(endIconMode, delegate);
}
return delegate;
}
private EndIconDelegate create(@EndIconMode int endIconMode) {
switch (endIconMode) {
case END_ICON_PASSWORD_TOGGLE:
return new PasswordToggleEndIconDelegate(
endLayout, endIconDrawableId == 0 ? passwordIconDrawableId : endIconDrawableId);
case END_ICON_CLEAR_TEXT:
return new ClearTextEndIconDelegate(endLayout, endIconDrawableId);
case END_ICON_DROPDOWN_MENU:
return new DropdownMenuEndIconDelegate(endLayout, endIconDrawableId);
case END_ICON_CUSTOM:
return new CustomEndIconDelegate(endLayout, endIconDrawableId);
case END_ICON_NONE:
return new NoEndIconDelegate(endLayout);
default:
throw new IllegalArgumentException("Invalid end icon mode: " + endIconMode);
}
}
}
}