Skip to content

Commit be8e4e2

Browse files
paulfthomashunterstich
authored andcommitted
[MaterialDatePicker] Improve error formatting
PiperOrigin-RevId: 459495629
1 parent 4e52469 commit be8e4e2

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,22 @@ abstract class DateFormatTextWatcher extends TextWatcherAdapter {
5252
this.constraints = constraints;
5353
this.outOfRange = textInputLayout.getContext().getString(R.string.mtrl_picker_out_of_range);
5454
setErrorCallback =
55-
new Runnable() {
56-
@Override
57-
public void run() {
58-
TextInputLayout textLayout = DateFormatTextWatcher.this.textInputLayout;
59-
DateFormat df = DateFormatTextWatcher.this.dateFormat;
60-
Context context = textLayout.getContext();
61-
String invalidFormat = context.getString(R.string.mtrl_picker_invalid_format);
62-
String useLine =
63-
String.format(
64-
context.getString(R.string.mtrl_picker_invalid_format_use), formatHint);
65-
String exampleLine =
66-
String.format(
67-
context.getString(R.string.mtrl_picker_invalid_format_example),
68-
df.format(new Date(UtcDates.getTodayCalendar().getTimeInMillis())));
69-
textLayout.setError(invalidFormat + "\n" + useLine + "\n" + exampleLine);
70-
onInvalidDate();
71-
}
55+
() -> {
56+
TextInputLayout textLayout = DateFormatTextWatcher.this.textInputLayout;
57+
DateFormat df = DateFormatTextWatcher.this.dateFormat;
58+
Context context = textLayout.getContext();
59+
String invalidFormat = context.getString(R.string.mtrl_picker_invalid_format);
60+
String useLine =
61+
String.format(
62+
context.getString(R.string.mtrl_picker_invalid_format_use),
63+
sanitizeDateString(formatHint));
64+
String exampleLine =
65+
String.format(
66+
context.getString(R.string.mtrl_picker_invalid_format_example),
67+
sanitizeDateString(
68+
df.format(new Date(UtcDates.getTodayCalendar().getTimeInMillis()))));
69+
textLayout.setError(invalidFormat + "\n" + useLine + "\n" + exampleLine);
70+
onInvalidDate();
7271
};
7372
}
7473

@@ -104,16 +103,18 @@ public void onTextChanged(@NonNull CharSequence s, int start, int before, int co
104103
}
105104

106105
private Runnable createRangeErrorCallback(final long milliseconds) {
107-
return new Runnable() {
108-
@Override
109-
public void run() {
110-
textInputLayout.setError(
111-
String.format(outOfRange, DateStrings.getDateString(milliseconds)));
112-
onInvalidDate();
113-
}
106+
return () -> {
107+
String dateString = DateStrings.getDateString(milliseconds);
108+
textInputLayout.setError(String.format(outOfRange, sanitizeDateString(dateString)));
109+
onInvalidDate();
114110
};
115111
}
116112

113+
private String sanitizeDateString(String dateString) {
114+
// Replace all regular spaces with non-breaking spaces so the date wraps as a single unit.
115+
return dateString.replace(' ', '\u00A0');
116+
}
117+
117118
public void runValidation(View view, Runnable validation) {
118119
view.postDelayed(validation, VALIDATION_DELAY);
119120
}

0 commit comments

Comments
 (0)