Skip to content

Commit

Permalink
[TextField] Fix helper text label for usage
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Material Design Team authored and drchen committed Nov 4, 2021
1 parent a455a53 commit aaac96d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -511,6 +528,11 @@ void setHelperTextEnabled(boolean enabled) {
helperTextEnabled = enabled;
}

@Nullable
View getHelperTextView() {
return helperTextView;
}

boolean errorIsDisplayed() {
return isCaptionStateError(captionDisplayed);
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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);
}
}
}
}
Expand Down

0 comments on commit aaac96d

Please sign in to comment.