Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a9552f4

Browse files
authored
Reland fixes Android text field to use hint text for accessibility (#37093)
1 parent 73664bc commit a9552f4

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

shell/platform/android/io/flutter/view/AccessibilityBridge.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,10 @@ && shouldSetCollectionInfo(semanticsNode)) {
885885
// Scopes routes are not focusable, only need to set the content
886886
// for non-scopes-routes semantics nodes.
887887
if (semanticsNode.hasFlag(Flag.IS_TEXT_FIELD)) {
888-
result.setText(semanticsNode.getValueLabelHint());
888+
result.setText(semanticsNode.getValue());
889+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
890+
result.setHintText(semanticsNode.getTextFieldHint());
891+
}
889892
} else if (!semanticsNode.hasFlag(Flag.SCOPES_ROUTE)) {
890893
CharSequence content = semanticsNode.getValueLabelHint();
891894
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
@@ -2773,18 +2776,47 @@ private float max(float a, float b, float c, float d) {
27732776
return Math.max(a, Math.max(b, Math.max(c, d)));
27742777
}
27752778

2776-
private CharSequence getValueLabelHint() {
2777-
CharSequence[] array;
2779+
private CharSequence getValue() {
2780+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
2781+
return value;
2782+
} else {
2783+
return createSpannableString(value, valueAttributes);
2784+
}
2785+
}
2786+
2787+
private CharSequence getLabel() {
27782788
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
2779-
array = new CharSequence[] {value, label, hint};
2789+
return label;
27802790
} else {
2781-
array =
2782-
new CharSequence[] {
2783-
createSpannableString(value, valueAttributes),
2784-
createSpannableString(label, labelAttributes),
2785-
createSpannableString(hint, hintAttributes),
2786-
};
2791+
return createSpannableString(label, labelAttributes);
2792+
}
2793+
}
2794+
2795+
private CharSequence getHint() {
2796+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
2797+
return hint;
2798+
} else {
2799+
return createSpannableString(hint, hintAttributes);
2800+
}
2801+
}
2802+
2803+
private CharSequence getValueLabelHint() {
2804+
CharSequence[] array = new CharSequence[] {getValue(), getLabel(), getHint()};
2805+
CharSequence result = null;
2806+
for (CharSequence word : array) {
2807+
if (word != null && word.length() > 0) {
2808+
if (result == null || result.length() == 0) {
2809+
result = word;
2810+
} else {
2811+
result = TextUtils.concat(result, ", ", word);
2812+
}
2813+
}
27872814
}
2815+
return result;
2816+
}
2817+
2818+
private CharSequence getTextFieldHint() {
2819+
CharSequence[] array = new CharSequence[] {getLabel(), getHint()};
27882820
CharSequence result = null;
27892821
for (CharSequence word : array) {
27902822
if (word != null && word.length() > 0) {

shell/platform/android/test/io/flutter/view/AccessibilityBridgeTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,23 @@ public void itDescribesNonTextFieldsWithAContentDescription() {
7676
assertEquals(nodeInfo.getText(), null);
7777
}
7878

79+
@TargetApi(26)
7980
@Test
80-
public void itDescribesTextFieldsWithText() {
81+
public void itDescribesTextFieldsWithTextAndHint() {
8182
AccessibilityBridge accessibilityBridge = setUpBridge();
8283

8384
TestSemanticsNode testSemanticsNode = new TestSemanticsNode();
84-
testSemanticsNode.label = "Hello, World";
85+
testSemanticsNode.value = "Hello, World";
86+
testSemanticsNode.label = "some label";
87+
testSemanticsNode.hint = "some hint";
8588
testSemanticsNode.addFlag(AccessibilityBridge.Flag.IS_TEXT_FIELD);
8689
TestSemanticsUpdate testSemanticsUpdate = testSemanticsNode.toUpdate();
8790
testSemanticsUpdate.sendUpdateToBridge(accessibilityBridge);
8891
AccessibilityNodeInfo nodeInfo = accessibilityBridge.createAccessibilityNodeInfo(0);
8992

9093
assertEquals(nodeInfo.getContentDescription(), null);
9194
assertEquals(nodeInfo.getText().toString(), "Hello, World");
95+
assertEquals(nodeInfo.getHintText().toString(), "some label, some hint");
9296
}
9397

9498
@Test

0 commit comments

Comments
 (0)