Skip to content

Commit a00ee50

Browse files
hunterstichdrchen
authored andcommittedOct 12, 2023
[DatePicker] Added builder methods for customizing the positive and negative button content descriptions
Resolves #3595 PiperOrigin-RevId: 572929926
1 parent bfee8a7 commit a00ee50

File tree

3 files changed

+119
-4
lines changed

3 files changed

+119
-4
lines changed
 

‎catalog/java/io/material/catalog/datepicker/DatePickerMainDemoFragment.java

+4
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,14 @@ public View onCreateDemoView(
147147

148148
if (positiveButtonChoice == R.id.cat_picker_positive_button_custom) {
149149
builder.setPositiveButtonText(R.string.cat_picker_positive_button_text);
150+
builder.setPositiveButtonContentDescription(
151+
R.string.cat_picker_positive_button_content_description);
150152
}
151153

152154
if (negativeButtonChoice == R.id.cat_picker_negative_button_custom) {
153155
builder.setNegativeButtonText(R.string.cat_picker_negative_button_text);
156+
builder.setNegativeButtonContentDescription(
157+
R.string.cat_picker_negative_button_content_description);
154158
}
155159

156160
setupDayViewDecorator(builder, dayViewDecoratorChoice);

‎catalog/java/io/material/catalog/datepicker/res/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@
137137

138138
<!-- The custom positive button text -->
139139
<string name="cat_picker_positive_button_text">DONE</string>
140+
<string name="cat_picker_positive_button_content_description">Finished</string>
140141
<!-- The custom negative button text -->
141142
<string name="cat_picker_negative_button_text">Negative</string>
143+
<string name="cat_picker_negative_button_content_description">Negative</string>
142144

143145
<!-- The custom dots day view decorator's content description [CHAR LIMIT=120] -->
144146
<string name="cat_picker_day_view_decorator_dots_content_description">%s Dotted</string>

‎lib/java/com/google/android/material/datepicker/MaterialDatePicker.java

+113-4
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,16 @@ public final class MaterialDatePicker<S> extends DialogFragment {
8484
private static final String TITLE_TEXT_KEY = "TITLE_TEXT_KEY";
8585
private static final String POSITIVE_BUTTON_TEXT_RES_ID_KEY = "POSITIVE_BUTTON_TEXT_RES_ID_KEY";
8686
private static final String POSITIVE_BUTTON_TEXT_KEY = "POSITIVE_BUTTON_TEXT_KEY";
87+
private static final String POSITIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY =
88+
"POSITIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY";
89+
private static final String POSITIVE_BUTTON_CONTENT_DESCRIPTION_KEY =
90+
"POSITIVE_BUTTON_CONTENT_DESCRIPTION_KEY";
8791
private static final String NEGATIVE_BUTTON_TEXT_RES_ID_KEY = "NEGATIVE_BUTTON_TEXT_RES_ID_KEY";
8892
private static final String NEGATIVE_BUTTON_TEXT_KEY = "NEGATIVE_BUTTON_TEXT_KEY";
93+
private static final String NEGATIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY =
94+
"NEGATIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY";
95+
private static final String NEGATIVE_BUTTON_CONTENT_DESCRIPTION_KEY =
96+
"NEGATIVE_BUTTON_CONTENT_DESCRIPTION_KEY";
8997
private static final String INPUT_MODE_KEY = "INPUT_MODE_KEY";
9098

9199
static final Object CONFIRM_BUTTON_TAG = "CONFIRM_BUTTON_TAG";
@@ -146,9 +154,12 @@ public String getHeaderText() {
146154
@InputMode private int inputMode;
147155
@StringRes private int positiveButtonTextResId;
148156
private CharSequence positiveButtonText;
157+
@StringRes private int positiveButtonContentDescriptionResId;
158+
private CharSequence positiveButtonContentDescription;
149159
@StringRes private int negativeButtonTextResId;
150160
private CharSequence negativeButtonText;
151-
161+
@StringRes private int negativeButtonContentDescriptionResId;
162+
private CharSequence negativeButtonContentDescription;
152163
private TextView headerTitleTextView;
153164
private TextView headerSelectionText;
154165
private CheckableImageButton headerToggleButton;
@@ -172,8 +183,18 @@ static <S> MaterialDatePicker<S> newInstance(@NonNull Builder<S> options) {
172183
args.putInt(INPUT_MODE_KEY, options.inputMode);
173184
args.putInt(POSITIVE_BUTTON_TEXT_RES_ID_KEY, options.positiveButtonTextResId);
174185
args.putCharSequence(POSITIVE_BUTTON_TEXT_KEY, options.positiveButtonText);
186+
args.putInt(
187+
POSITIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY,
188+
options.positiveButtonContentDescriptionResId);
189+
args.putCharSequence(
190+
POSITIVE_BUTTON_CONTENT_DESCRIPTION_KEY, options.positiveButtonContentDescription);
175191
args.putInt(NEGATIVE_BUTTON_TEXT_RES_ID_KEY, options.negativeButtonTextResId);
176192
args.putCharSequence(NEGATIVE_BUTTON_TEXT_KEY, options.negativeButtonText);
193+
args.putInt(
194+
NEGATIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY,
195+
options.negativeButtonContentDescriptionResId);
196+
args.putCharSequence(
197+
NEGATIVE_BUTTON_CONTENT_DESCRIPTION_KEY, options.negativeButtonContentDescription);
177198
materialDatePickerDialogFragment.setArguments(args);
178199
return materialDatePickerDialogFragment;
179200
}
@@ -197,8 +218,16 @@ public final void onSaveInstanceState(@NonNull Bundle bundle) {
197218
bundle.putInt(INPUT_MODE_KEY, inputMode);
198219
bundle.putInt(POSITIVE_BUTTON_TEXT_RES_ID_KEY, positiveButtonTextResId);
199220
bundle.putCharSequence(POSITIVE_BUTTON_TEXT_KEY, positiveButtonText);
221+
bundle.putInt(
222+
POSITIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY, positiveButtonContentDescriptionResId);
223+
bundle.putCharSequence(
224+
POSITIVE_BUTTON_CONTENT_DESCRIPTION_KEY, positiveButtonContentDescription);
200225
bundle.putInt(NEGATIVE_BUTTON_TEXT_RES_ID_KEY, negativeButtonTextResId);
201226
bundle.putCharSequence(NEGATIVE_BUTTON_TEXT_KEY, negativeButtonText);
227+
bundle.putInt(
228+
NEGATIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY, negativeButtonContentDescriptionResId);
229+
bundle.putCharSequence(
230+
NEGATIVE_BUTTON_CONTENT_DESCRIPTION_KEY, negativeButtonContentDescription);
202231
}
203232

204233
@Override
@@ -214,8 +243,16 @@ public final void onCreate(@Nullable Bundle bundle) {
214243
inputMode = activeBundle.getInt(INPUT_MODE_KEY);
215244
positiveButtonTextResId = activeBundle.getInt(POSITIVE_BUTTON_TEXT_RES_ID_KEY);
216245
positiveButtonText = activeBundle.getCharSequence(POSITIVE_BUTTON_TEXT_KEY);
246+
positiveButtonContentDescriptionResId =
247+
activeBundle.getInt(POSITIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY);
248+
positiveButtonContentDescription =
249+
activeBundle.getCharSequence(POSITIVE_BUTTON_CONTENT_DESCRIPTION_KEY);
217250
negativeButtonTextResId = activeBundle.getInt(NEGATIVE_BUTTON_TEXT_RES_ID_KEY);
218251
negativeButtonText = activeBundle.getCharSequence(NEGATIVE_BUTTON_TEXT_KEY);
252+
negativeButtonContentDescriptionResId =
253+
activeBundle.getInt(NEGATIVE_BUTTON_CONTENT_DESCRIPTION_RES_ID_KEY);
254+
negativeButtonContentDescription =
255+
activeBundle.getCharSequence(NEGATIVE_BUTTON_CONTENT_DESCRIPTION_KEY);
219256

220257
fullTitleText =
221258
titleText != null ? titleText : requireContext().getResources().getText(titleTextResId);
@@ -302,6 +339,12 @@ public final View onCreateView(
302339
} else if (positiveButtonTextResId != 0) {
303340
confirmButton.setText(positiveButtonTextResId);
304341
}
342+
if (positiveButtonContentDescription != null) {
343+
confirmButton.setContentDescription(positiveButtonContentDescription);
344+
} else if (positiveButtonContentDescriptionResId != 0) {
345+
confirmButton.setContentDescription(
346+
getContext().getResources().getText(positiveButtonContentDescriptionResId));
347+
}
305348
confirmButton.setOnClickListener(
306349
new View.OnClickListener() {
307350
@Override
@@ -321,6 +364,12 @@ public void onClick(View v) {
321364
} else if (negativeButtonTextResId != 0) {
322365
cancelButton.setText(negativeButtonTextResId);
323366
}
367+
if (negativeButtonContentDescription != null) {
368+
cancelButton.setContentDescription(negativeButtonContentDescription);
369+
} else if (negativeButtonContentDescriptionResId != 0) {
370+
cancelButton.setContentDescription(
371+
getContext().getResources().getText(negativeButtonContentDescriptionResId));
372+
}
324373
cancelButton.setOnClickListener(
325374
new View.OnClickListener() {
326375
@Override
@@ -403,8 +452,7 @@ private void enableEdgeToEdgeIfNeeded(Window window) {
403452
return;
404453
}
405454
final View headerLayout = requireView().findViewById(R.id.fullscreen_header);
406-
EdgeToEdgeUtils.applyEdgeToEdge(
407-
window, true, ViewUtils.getBackgroundColor(headerLayout), null);
455+
EdgeToEdgeUtils.applyEdgeToEdge(window, true, ViewUtils.getBackgroundColor(headerLayout), null);
408456
final int originalPaddingTop = headerLayout.getPaddingTop();
409457
final int originalHeaderHeight = headerLayout.getLayoutParams().height;
410458
ViewCompat.setOnApplyWindowInsetsListener(
@@ -662,8 +710,12 @@ public static final class Builder<S> {
662710
CharSequence titleText = null;
663711
int positiveButtonTextResId = 0;
664712
CharSequence positiveButtonText = null;
713+
int positiveButtonContentDescriptionResId = 0;
714+
CharSequence positiveButtonContentDescription = null;
665715
int negativeButtonTextResId = 0;
666716
CharSequence negativeButtonText = null;
717+
int negativeButtonContentDescriptionResId = 0;
718+
CharSequence negativeButtonContentDescription = null;
667719
@Nullable S selection = null;
668720
@InputMode int inputMode = INPUT_MODE_CALENDAR;
669721

@@ -797,6 +849,34 @@ public Builder<S> setPositiveButtonText(@Nullable CharSequence text) {
797849
return this;
798850
}
799851

852+
/**
853+
* Sets the content description used in the positive button
854+
*
855+
* @param contentDescriptionId resource id to be used as content description in the positive
856+
* button
857+
*/
858+
@NonNull
859+
@CanIgnoreReturnValue
860+
public Builder<S> setPositiveButtonContentDescription(@StringRes int contentDescriptionId) {
861+
this.positiveButtonContentDescriptionResId = contentDescriptionId;
862+
this.positiveButtonContentDescription = null;
863+
return this;
864+
}
865+
866+
/**
867+
* Sets the content description used in the positive button
868+
*
869+
* @param contentDescription content description used in the positive button
870+
*/
871+
@NonNull
872+
@CanIgnoreReturnValue
873+
public Builder<S> setPositiveButtonContentDescription(
874+
@Nullable CharSequence contentDescription) {
875+
this.positiveButtonContentDescription = contentDescription;
876+
this.positiveButtonContentDescriptionResId = 0;
877+
return this;
878+
}
879+
800880
/**
801881
* Sets the text used in the negative button
802882
*
@@ -823,6 +903,34 @@ public Builder<S> setNegativeButtonText(@Nullable CharSequence text) {
823903
return this;
824904
}
825905

906+
/**
907+
* Sets the content description used in the negative button
908+
*
909+
* @param contentDescriptionId resource id to be used as content description in the negative
910+
* button
911+
*/
912+
@NonNull
913+
@CanIgnoreReturnValue
914+
public Builder<S> setNegativeButtonContentDescription(@StringRes int contentDescriptionId) {
915+
this.negativeButtonContentDescriptionResId = contentDescriptionId;
916+
this.negativeButtonContentDescription = null;
917+
return this;
918+
}
919+
920+
/**
921+
* Sets the content description used in the negative button
922+
*
923+
* @param contentDescription content description used in the negative button
924+
*/
925+
@NonNull
926+
@CanIgnoreReturnValue
927+
public Builder<S> setNegativeButtonContentDescription(
928+
@Nullable CharSequence contentDescription) {
929+
this.negativeButtonContentDescription = contentDescription;
930+
this.negativeButtonContentDescriptionResId = 0;
931+
return this;
932+
}
933+
826934
/** Sets the input mode to start with. */
827935
@NonNull
828936
@CanIgnoreReturnValue
@@ -864,7 +972,8 @@ private Month createDefaultOpenAt() {
864972

865973
Month thisMonth = Month.current();
866974
return monthInValidRange(thisMonth, calendarConstraints)
867-
? thisMonth : calendarConstraints.getStart();
975+
? thisMonth
976+
: calendarConstraints.getStart();
868977
}
869978

870979
private static boolean monthInValidRange(Month month, CalendarConstraints constraints) {

0 commit comments

Comments
 (0)
Please sign in to comment.