Skip to content

Commit 2701161

Browse files
leticiarossiveganafro
authored andcommitted
[a11y][TextInputLayout] Removed TextInputLayout's placeholder text duplicated announcement, and fixed edge case where placeholder text was being enabled and added to the input frame while it was set to null.
Before, when swiping with TalkBack on, the placeholder text would be mistakenly announced. This change makes it so it's only announced when it appears, for API >= 16. It's still announced as part of the description announcement when the text field is selected, as before, so it's still accessible for APIs < 16. PiperOrigin-RevId: 404871534
1 parent abb9d5b commit 2701161

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,8 +2266,22 @@ private static void updateCounterContentDescription(
22662266
* @see #getPlaceholderText()
22672267
*/
22682268
public void setPlaceholderText(@Nullable final CharSequence placeholderText) {
2269-
// If placeholder text is null, disable placeholder if it's enabled.
2270-
if (placeholderEnabled && TextUtils.isEmpty(placeholderText)) {
2269+
if (placeholderTextView == null) {
2270+
placeholderTextView = new AppCompatTextView(getContext());
2271+
placeholderTextView.setId(R.id.textinput_placeholder);
2272+
ViewCompat.setImportantForAccessibility(
2273+
placeholderTextView, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
2274+
2275+
placeholderFadeIn = createPlaceholderFadeTransition();
2276+
placeholderFadeIn.setStartDelay(PLACEHOLDER_START_DELAY);
2277+
placeholderFadeOut = createPlaceholderFadeTransition();
2278+
2279+
setPlaceholderTextAppearance(placeholderTextAppearance);
2280+
setPlaceholderTextColor(placeholderTextColor);
2281+
}
2282+
2283+
// If placeholder text is null, disable placeholder.
2284+
if (TextUtils.isEmpty(placeholderText)) {
22712285
setPlaceholderTextEnabled(false);
22722286
} else {
22732287
if (!placeholderEnabled) {
@@ -2298,19 +2312,6 @@ private void setPlaceholderTextEnabled(boolean placeholderEnabled) {
22982312

22992313
// Otherwise, adjust enabled state.
23002314
if (placeholderEnabled) {
2301-
placeholderTextView = new AppCompatTextView(getContext());
2302-
placeholderTextView.setId(R.id.textinput_placeholder);
2303-
2304-
placeholderFadeIn = createPlaceholderFadeTransition();
2305-
placeholderFadeIn.setStartDelay(PLACEHOLDER_START_DELAY);
2306-
2307-
placeholderFadeOut = createPlaceholderFadeTransition();
2308-
2309-
ViewCompat.setAccessibilityLiveRegion(
2310-
placeholderTextView, ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE);
2311-
2312-
setPlaceholderTextAppearance(placeholderTextAppearance);
2313-
setPlaceholderTextColor(placeholderTextColor);
23142315
addPlaceholderTextView();
23152316
} else {
23162317
removePlaceholderTextView();
@@ -2344,6 +2345,9 @@ private void showPlaceholderText() {
23442345
TransitionManager.beginDelayedTransition(inputFrame, placeholderFadeIn);
23452346
placeholderTextView.setVisibility(VISIBLE);
23462347
placeholderTextView.bringToFront();
2348+
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
2349+
announceForAccessibility(placeholderText);
2350+
}
23472351
}
23482352
}
23492353

tests/javatests/com/google/android/material/textfield/TextInputLayoutTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,11 @@ public void testDispatchProvideAutofillStructure() {
361361
final ViewStructureImpl structure = new ViewStructureImpl();
362362
layout.dispatchProvideAutofillStructure(structure, 0);
363363

364-
// 1 x EditText, 3 x TextView (prefix/suffix/placeholder).
365-
assertEquals(4, structure.getChildCount());
364+
// 1 x EditText, 2 x TextView (prefix/suffix).
365+
assertEquals(3, structure.getChildCount());
366366

367367
// Asserts the structure.
368-
ViewStructureImpl childStructure = structure.getChildAt(1);
368+
ViewStructureImpl childStructure = structure.getChildAt(0);
369369
assertEquals(EditText.class.getName(), childStructure.getClassName());
370370
assertEquals("Nested hint", childStructure.getHint().toString());
371371

@@ -751,7 +751,7 @@ public void hintSetOnNestedEditText_propagateInnerHintToAutofillProvider() {
751751

752752
textInputLayout.dispatchProvideAutofillStructure(structure, /* flags= */ 0);
753753

754-
final ViewStructureImpl editText = structure.getChildAt(1);
754+
final ViewStructureImpl editText = structure.getChildAt(0);
755755
assertEquals(EditText.class.getName(), editText.getClassName());
756756
assertEquals(structure.getAutofillId(), textInputLayout.getAutofillId());
757757
assertEquals("Nested hint", editText.getHint().toString());
@@ -767,7 +767,7 @@ public void hintSetOnOuterLayout_propagateOuterHintToAutofillProvider() {
767767

768768
textInputLayout.dispatchProvideAutofillStructure(structure, /* flags= */ 0);
769769

770-
final ViewStructureImpl editText = structure.getChildAt(1);
770+
final ViewStructureImpl editText = structure.getChildAt(0);
771771
assertEquals(EditText.class.getName(), editText.getClassName());
772772
assertEquals(structure.getAutofillId(), textInputLayout.getAutofillId());
773773
assertEquals("Outer hint", editText.getHint().toString());

0 commit comments

Comments
 (0)