Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DatePicker] Fix date input hints #4481

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions lib/java/com/google/android/material/datepicker/UtcDates.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,75 @@ static SimpleDateFormat getDefaultTextInputFormat() {
}

static String getDefaultTextInputHint(Resources res, SimpleDateFormat format) {
String formatHint = format.toPattern();
String yearChar = res.getString(R.string.mtrl_picker_text_input_year_abbr);
String monthChar = res.getString(R.string.mtrl_picker_text_input_month_abbr);
String dayChar = res.getString(R.string.mtrl_picker_text_input_day_abbr);

// Remove duplicate characters for Korean.
if (Locale.getDefault().getLanguage().equals(Locale.KOREAN.getLanguage())) {
formatHint = formatHint.replaceAll("d+", "d").replaceAll("M+", "M").replaceAll("y+", "y");
String datePattern = format.toPattern();

final String yearAbbr = res.getString(R.string.mtrl_picker_text_input_year_abbr);
final String monthAbbr = res.getString(R.string.mtrl_picker_text_input_month_abbr);
final String dayAbbr = res.getString(R.string.mtrl_picker_text_input_day_abbr);

final boolean abbrsArePlaceholders =
res.getBoolean(R.bool.mtrl_picker_text_input_hint_abbrs_are_placeholders);

return abbrsArePlaceholders
? replaceOneLetterFormatSpecifiers(datePattern, yearAbbr, monthAbbr, dayAbbr)
: replaceFormatSpecifiers(datePattern, yearAbbr, monthAbbr, dayAbbr);
}

private static String replaceOneLetterFormatSpecifiers(
String datePattern, String yearAbbr, String monthAbbr, String dayAbbr) {
final StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < datePattern.length(); i++) {
char ch = datePattern.charAt(i);
switch (ch) {
case 'y':
stringBuilder.append(yearAbbr);
break;
case 'M':
stringBuilder.append(monthAbbr);
break;
case 'd':
stringBuilder.append(dayAbbr);
break;
default:
stringBuilder.append(ch);
break;
}
}

return stringBuilder.toString();
}

private static String replaceFormatSpecifiers(
String datePattern, String yearAbbr, String monthAbbr, String dayAbbr) {
final StringBuilder stringBuilder = new StringBuilder();

char prevCh = 0;
for (int i = 0; i < datePattern.length(); i++) {
char ch = datePattern.charAt(i);
switch (ch) {
case 'y':
if (prevCh != 'y') {
stringBuilder.append(yearAbbr);
}
break;
case 'M':
if (prevCh != 'M') {
stringBuilder.append(monthAbbr);
}
break;
case 'd':
if (prevCh != 'd') {
stringBuilder.append(dayAbbr);
}
break;
default:
stringBuilder.append(ch);
break;
}
prevCh = ch;
}

return formatHint.replace("d", dayChar).replace("M", monthChar).replace("y", yearChar);
return stringBuilder.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<resources>
<bool name="mtrl_picker_text_input_hint_abbrs_are_placeholders">false</bool>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<resources>
<bool name="mtrl_picker_text_input_hint_abbrs_are_placeholders">false</bool>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<resources>
<bool name="mtrl_picker_text_input_hint_abbrs_are_placeholders">false</bool>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2024 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<resources>
<bool name="mtrl_picker_text_input_hint_abbrs_are_placeholders">true</bool>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ public void textInputHintWith1CharYearLocalized() {
assertEquals("m/j/a", hint);
}

@Test
@Config(qualifiers = "az")
public void textInputHintForAzerbaijani() {
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy");
String hint = UtcDates.getDefaultTextInputHint(context.getResources(), sdf);

assertEquals("gün.ay.il", hint);
}

@Test
@Config(qualifiers = "ko")
public void textInputHintForKorean() {
Expand All @@ -87,6 +96,15 @@ public void textInputHintForKorean() {
assertEquals("년.월.일.", hint);
}

@Test
@Config(qualifiers = "zh-rHK")
public void textInputHintForChineseHongKong() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
String hint = UtcDates.getDefaultTextInputHint(context.getResources(), sdf);

assertEquals("日/月/年", hint);
}

@Test
public void normalizeTextInputFormat() {
SimpleDateFormat sdf = new SimpleDateFormat("M/d/y");
Expand Down