From aaac96d724effb770d494037eca86e3f9f4c0701 Mon Sep 17 00:00:00 2001 From: Material Design Team Date: Tue, 2 Nov 2021 17:29:17 -0400 Subject: [PATCH] [TextField] Fix helper text label for usage The setLabelFor calls were made in the call to populate the accessibility node but they were made after calling super (one of them was actually nested inside of a getter!), this is too late, the accessibility node is populated with the label for by the super class PiperOrigin-RevId: 407178334 --- .../textfield/IndicatorViewController.java | 22 +++++++++++++++++++ .../material/textfield/TextInputEditText.java | 5 ----- .../material/textfield/TextInputLayout.java | 7 ++++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/lib/java/com/google/android/material/textfield/IndicatorViewController.java b/lib/java/com/google/android/material/textfield/IndicatorViewController.java index ddd2f236bca..cfc3b075cfa 100644 --- a/lib/java/com/google/android/material/textfield/IndicatorViewController.java +++ b/lib/java/com/google/android/material/textfield/IndicatorViewController.java @@ -29,13 +29,16 @@ import android.content.res.ColorStateList; import android.graphics.Typeface; import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; import androidx.core.view.ViewCompat; import androidx.core.widget.TextViewCompat; import androidx.appcompat.widget.AppCompatTextView; import android.text.TextUtils; import android.view.View; +import android.view.View.AccessibilityDelegate; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; +import android.view.accessibility.AccessibilityNodeInfo; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.LinearLayout; @@ -501,6 +504,20 @@ void setHelperTextEnabled(boolean enabled) { setHelperTextAppearance(helperTextTextAppearance); setHelperTextViewTextColor(helperTextViewTextColor); addIndicator(helperTextView, HELPER_INDEX); + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { + helperTextView.setAccessibilityDelegate( + new AccessibilityDelegate() { + @Override + public void onInitializeAccessibilityNodeInfo( + View view, AccessibilityNodeInfo accessibilityNodeInfo) { + super.onInitializeAccessibilityNodeInfo(view, accessibilityNodeInfo); + View editText = textInputView.getEditText(); + if (editText != null) { + accessibilityNodeInfo.setLabeledBy(editText); + } + } + }); + } } else { hideHelperText(); removeIndicator(helperTextView, HELPER_INDEX); @@ -511,6 +528,11 @@ void setHelperTextEnabled(boolean enabled) { helperTextEnabled = enabled; } + @Nullable + View getHelperTextView() { + return helperTextView; + } + boolean errorIsDisplayed() { return isCaptionStateError(captionDisplayed); } diff --git a/lib/java/com/google/android/material/textfield/TextInputEditText.java b/lib/java/com/google/android/material/textfield/TextInputEditText.java index fbc0531277a..eda50d72731 100644 --- a/lib/java/com/google/android/material/textfield/TextInputEditText.java +++ b/lib/java/com/google/android/material/textfield/TextInputEditText.java @@ -25,7 +25,6 @@ import android.graphics.Point; import android.graphics.Rect; import android.os.Build.VERSION; -import android.os.Build.VERSION_CODES; import androidx.appcompat.widget.AppCompatEditText; import android.text.TextUtils; import android.util.AttributeSet; @@ -208,10 +207,6 @@ private String getAccessibilityNodeInfoText(@NonNull TextInputLayout layout) { boolean showingText = !TextUtils.isEmpty(inputText); boolean hasHint = !TextUtils.isEmpty(hintText); - if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { - setLabelFor(R.id.textinput_helper_text); - } - String hint = hasHint ? hintText.toString() : ""; if (showingText) { diff --git a/lib/java/com/google/android/material/textfield/TextInputLayout.java b/lib/java/com/google/android/material/textfield/TextInputLayout.java index 91562694c29..60527e25bb5 100644 --- a/lib/java/com/google/android/material/textfield/TextInputLayout.java +++ b/lib/java/com/google/android/material/textfield/TextInputLayout.java @@ -4458,8 +4458,11 @@ public void onInitializeAccessibilityNodeInfo( info.setError(showingError ? errorText : counterOverflowDesc); } - if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1 && editText != null) { - editText.setLabelFor(R.id.textinput_helper_text); + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) { + View helperTextView = layout.indicatorViewController.getHelperTextView(); + if (helperTextView != null) { + info.setLabelFor(helperTextView); + } } } }