Skip to content

Commit 7c162ef

Browse files
kendrickumstattdhunterstich
authored andcommitted
[DatePicker] Fix Talkback issues with the hint text by adding a TtsSpan.TYPE_VERBATIM so that the hints are read as verbatim text by screen readers.
PiperOrigin-RevId: 776123945
1 parent 05dc805 commit 7c162ef

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

lib/java/com/google/android/material/datepicker/RangeDateSelector.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.os.Parcel;
2424
import android.os.Parcelable;
2525
import android.text.InputType;
26+
import android.text.SpannableString;
2627
import android.text.TextUtils;
2728
import android.util.DisplayMetrics;
2829
import android.view.LayoutInflater;
@@ -251,8 +252,10 @@ public View onCreateTextInputView(
251252
? format.toPattern()
252253
: UtcDates.getDefaultTextInputHint(root.getResources(), format);
253254

254-
startTextInput.setPlaceholderText(formatHint);
255-
endTextInput.setPlaceholderText(formatHint);
255+
SpannableString verbatimHint = UtcDates.getVerbatimTextInputHint(formatHint);
256+
257+
startTextInput.setPlaceholderText(verbatimHint);
258+
endTextInput.setPlaceholderText(verbatimHint);
256259

257260
startEditText.addTextChangedListener(
258261
new DateFormatTextWatcher(formatHint, format, startTextInput, constraints) {

lib/java/com/google/android/material/datepicker/SingleDateSelector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public View onCreateTextInputView(
132132
? format.toPattern()
133133
: UtcDates.getDefaultTextInputHint(root.getResources(), format);
134134

135-
dateTextInput.setPlaceholderText(formatHint);
135+
dateTextInput.setPlaceholderText(UtcDates.getVerbatimTextInputHint(formatHint));
136136
if (selectedItem != null) {
137137
dateEditText.setText(format.format(selectedItem));
138138
// Move the cursor to the end of the text field

lib/java/com/google/android/material/datepicker/UtcDates.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import android.content.res.Resources;
2222
import android.icu.text.DisplayContext;
2323
import android.os.Build.VERSION_CODES;
24+
import android.text.SpannableString;
25+
import android.text.Spanned;
26+
import android.text.style.TtsSpan;
2427
import androidx.annotation.NonNull;
2528
import androidx.annotation.Nullable;
2629
import java.text.DateFormat;
@@ -180,6 +183,20 @@ static String getDefaultTextInputHint(Resources res, SimpleDateFormat format) {
180183
return formatHint.replace("d", dayChar).replace("M", monthChar).replace("y", yearChar);
181184
}
182185

186+
/**
187+
* Returns a SpannableString with the given format hint that has a TtsSpan.TYPE_VERBATIM span
188+
* applied to it in order to ensure that the hint is read verbatim by screen readers.
189+
*/
190+
static SpannableString getVerbatimTextInputHint(String formatHint) {
191+
SpannableString spannableHint = new SpannableString(formatHint);
192+
spannableHint.setSpan(
193+
new TtsSpan.Builder(TtsSpan.TYPE_VERBATIM).build(),
194+
0,
195+
spannableHint.length(),
196+
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
197+
return spannableHint;
198+
}
199+
183200
/**
184201
* Receives a given local date format string and returns a string that can be displayed to the
185202
* user and parsed by the date parser.

lib/javatests/com/google/android/material/datepicker/UtcDatesTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import android.content.Context;
2323
import androidx.appcompat.app.AppCompatActivity;
24+
import android.text.SpannableString;
2425
import androidx.test.core.app.ApplicationProvider;
2526
import java.text.DateFormat;
2627
import java.text.SimpleDateFormat;
@@ -87,6 +88,15 @@ public void textInputHintForKorean() {
8788
assertEquals("년.월.일.", hint);
8889
}
8990

91+
@Test
92+
public void verbatimTextInputHintForUK() {
93+
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy", Locale.UK);
94+
String hint = UtcDates.getDefaultTextInputHint(context.getResources(), sdf);
95+
SpannableString hintSpannable = UtcDates.getVerbatimTextInputHint(hint);
96+
97+
assertEquals("dd/mm/yyyy", hintSpannable.toString());
98+
}
99+
90100
@Test
91101
public void normalizeTextInputFormat() {
92102
SimpleDateFormat sdf = new SimpleDateFormat("M/d/y");

0 commit comments

Comments
 (0)