Skip to content

Commit

Permalink
[TextInputLayout][a11y] Changing hint announcement.
Browse files Browse the repository at this point in the history
* To make announcements clearer, removed helper text from hint announcement.
* Calling setLabelFor on helper text view so it's clear it refers to the edit text visually above it.
* Calling setError is enough to let the screen reader decide how to announce the error.

PiperOrigin-RevId: 317651946
  • Loading branch information
leticiarossi authored and ymarian committed Jun 26, 2020
1 parent 023e518 commit b274a4a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
Expand Down Expand Up @@ -204,16 +205,14 @@ public void onInitializeAccessibilityNodeInfo(@NonNull AccessibilityNodeInfo inf
private String getAccessibilityNodeInfoText(@NonNull TextInputLayout layout) {
CharSequence inputText = getText();
CharSequence hintText = layout.getHint();
CharSequence helperText = layout.getHelperText();
CharSequence errorText = layout.getError();
boolean showingText = !TextUtils.isEmpty(inputText);
boolean hasHint = !TextUtils.isEmpty(hintText);
boolean hasHelperText = !TextUtils.isEmpty(helperText);
boolean showingError = !TextUtils.isEmpty(errorText);

if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
setLabelFor(R.id.textinput_helper_text);
}

String hint = hasHint ? hintText.toString() : "";
hint += ((showingError || hasHelperText) && !TextUtils.isEmpty(hint)) ? ", " : "";
hint += showingError ? errorText : (hasHelperText ? helperText : "");

if (showingText) {
return inputText + (!TextUtils.isEmpty(hint) ? (", " + hint) : "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4096,19 +4096,15 @@ public void onInitializeAccessibilityNodeInfo(
EditText editText = layout.getEditText();
CharSequence inputText = (editText != null) ? editText.getText() : null;
CharSequence hintText = layout.getHint();
CharSequence helperText = layout.getHelperText();
CharSequence errorText = layout.getError();
int maxCharLimit = layout.getCounterMaxLength();
CharSequence counterOverflowDesc = layout.getCounterOverflowDescription();
boolean showingText = !TextUtils.isEmpty(inputText);
boolean hasHint = !TextUtils.isEmpty(hintText);
boolean hasHelperText = !TextUtils.isEmpty(helperText);
boolean showingError = !TextUtils.isEmpty(errorText);
boolean contentInvalid = showingError || !TextUtils.isEmpty(counterOverflowDesc);

String hint = hasHint ? hintText.toString() : "";
hint += ((showingError || hasHelperText) && !TextUtils.isEmpty(hint)) ? ", " : "";
hint += showingError ? errorText : (hasHelperText ? helperText : "");

if (showingText) {
info.setText(inputText);
Expand All @@ -4135,6 +4131,10 @@ public void onInitializeAccessibilityNodeInfo(
if (contentInvalid) {
info.setError(showingError ? errorText : counterOverflowDesc);
}

if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1 && editText != null) {
editText.setLabelFor(R.id.textinput_helper_text);
}
}
}
}

0 comments on commit b274a4a

Please sign in to comment.