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

Commit f7ed46a

Browse files
authored
Fixes Android text field to use hint text for accessibility (#36846)
1 parent d61031b commit f7ed46a

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

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

+40-10
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,8 @@ && 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+
result.setHintText(semanticsNode.getTextFieldHint());
889890
} else if (!semanticsNode.hasFlag(Flag.SCOPES_ROUTE)) {
890891
CharSequence content = semanticsNode.getValueLabelHint();
891892
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
@@ -2773,18 +2774,47 @@ private float max(float a, float b, float c, float d) {
27732774
return Math.max(a, Math.max(b, Math.max(c, d)));
27742775
}
27752776

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

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,21 @@ public void itDescribesNonTextFieldsWithAContentDescription() {
7777
}
7878

7979
@Test
80-
public void itDescribesTextFieldsWithText() {
80+
public void itDescribesTextFieldsWithTextAndHint() {
8181
AccessibilityBridge accessibilityBridge = setUpBridge();
8282

8383
TestSemanticsNode testSemanticsNode = new TestSemanticsNode();
84-
testSemanticsNode.label = "Hello, World";
84+
testSemanticsNode.value = "Hello, World";
85+
testSemanticsNode.label = "some label";
86+
testSemanticsNode.hint = "some hint";
8587
testSemanticsNode.addFlag(AccessibilityBridge.Flag.IS_TEXT_FIELD);
8688
TestSemanticsUpdate testSemanticsUpdate = testSemanticsNode.toUpdate();
8789
testSemanticsUpdate.sendUpdateToBridge(accessibilityBridge);
8890
AccessibilityNodeInfo nodeInfo = accessibilityBridge.createAccessibilityNodeInfo(0);
8991

9092
assertEquals(nodeInfo.getContentDescription(), null);
9193
assertEquals(nodeInfo.getText().toString(), "Hello, World");
94+
assertEquals(nodeInfo.getHintText().toString(), "some label, some hint");
9295
}
9396

9497
@Test

0 commit comments

Comments
 (0)