Skip to content

Commit ab5a405

Browse files
committed
TextView should report in an AccessibilityNodeInfo's text its hint if the View text is empty.
1. TextView was reporting only its text as the text of the emmited AccessibilityNodeInfo not using the hint if the text is empty which is inconsistent with the handling of accessibility events. 2. TextView was using the contentDescription in AccessibilityEvents if its text and hint are empyty. However, the content description is reported as a separate event property and it is incorrect to report it as the text. bug:5129138 Change-Id: I1c9270459cb07650a0ec5181cc4a01993dde3b5c
1 parent f1189e9 commit ab5a405

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

core/java/android/widget/TextView.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8939,14 +8939,8 @@ public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
89398939

89408940
final boolean isPassword = hasPasswordTransformationMethod();
89418941
if (!isPassword) {
8942-
CharSequence text = getText();
8942+
CharSequence text = getTextForAccessibility();
89438943
if (TextUtils.isEmpty(text)) {
8944-
text = getHint();
8945-
}
8946-
if (TextUtils.isEmpty(text)) {
8947-
text = getContentDescription();
8948-
}
8949-
if (!TextUtils.isEmpty(text)) {
89508944
event.getText().add(text);
89518945
}
89528946
}
@@ -8972,7 +8966,7 @@ public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
89728966

89738967
final boolean isPassword = hasPasswordTransformationMethod();
89748968
if (!isPassword) {
8975-
info.setText(getText());
8969+
info.setText(getTextForAccessibility());
89768970
}
89778971
info.setPassword(isPassword);
89788972
}
@@ -8988,6 +8982,20 @@ public void sendAccessibilityEvent(int eventType) {
89888982
super.sendAccessibilityEvent(eventType);
89898983
}
89908984

8985+
/**
8986+
* Gets the text reported for accessibility purposes. It is the
8987+
* text if not empty or the hint.
8988+
*
8989+
* @return The accessibility text.
8990+
*/
8991+
private CharSequence getTextForAccessibility() {
8992+
CharSequence text = getText();
8993+
if (TextUtils.isEmpty(text)) {
8994+
text = getHint();
8995+
}
8996+
return text;
8997+
}
8998+
89918999
void sendAccessibilityEventTypeViewTextChanged(CharSequence beforeText,
89929000
int fromIndex, int removedCount, int addedCount) {
89939001
AccessibilityEvent event =

0 commit comments

Comments
 (0)