diff --git a/catalog/java/io/material/catalog/datepicker/DatePickerMainDemoFragment.java b/catalog/java/io/material/catalog/datepicker/DatePickerMainDemoFragment.java index 2a217684b5e..94c3db4673e 100644 --- a/catalog/java/io/material/catalog/datepicker/DatePickerMainDemoFragment.java +++ b/catalog/java/io/material/catalog/datepicker/DatePickerMainDemoFragment.java @@ -100,6 +100,8 @@ public View onCreateDemoView( final RadioGroup opening = root.findViewById(R.id.cat_picker_opening_month_group); final RadioGroup selection = root.findViewById(R.id.cat_picker_selection_group); final RadioGroup inputMode = root.findViewById(R.id.cat_picker_input_mode_group); + final RadioGroup positiveButton = root.findViewById(R.id.cat_picker_positive_button_group); + final RadioGroup negativeButton = root.findViewById(R.id.cat_picker_negative_button_group); launcher.setOnClickListener( v -> { @@ -112,6 +114,8 @@ public View onCreateDemoView( int openingChoice = opening.getCheckedRadioButtonId(); int selectionChoice = selection.getCheckedRadioButtonId(); int inputModeChoices = inputMode.getCheckedRadioButtonId(); + int positiveButtonChoice = positiveButton.getCheckedRadioButtonId(); + int negativeButtonChoice = negativeButton.getCheckedRadioButtonId(); MaterialDatePicker.Builder builder = setupDateSelectorBuilder(selectionModeChoice, selectionChoice, inputModeChoices); @@ -130,6 +134,14 @@ public View onCreateDemoView( builder.setTitleText(R.string.cat_picker_title_custom); } + if (positiveButtonChoice == R.id.cat_picker_positive_button_custom) { + builder.setPositiveButtonText(R.string.cat_picker_positive_button_text); + } + + if (negativeButtonChoice == R.id.cat_picker_negative_button_custom) { + builder.setNegativeButtonText(R.string.cat_picker_negative_button_text); + } + try { builder.setCalendarConstraints(constraintsBuilder.build()); MaterialDatePicker picker = builder.build(); diff --git a/catalog/java/io/material/catalog/datepicker/res/layout/cat_picker_negative_button.xml b/catalog/java/io/material/catalog/datepicker/res/layout/cat_picker_negative_button.xml new file mode 100644 index 00000000000..f2748eba53a --- /dev/null +++ b/catalog/java/io/material/catalog/datepicker/res/layout/cat_picker_negative_button.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + diff --git a/catalog/java/io/material/catalog/datepicker/res/layout/cat_picker_positive_button.xml b/catalog/java/io/material/catalog/datepicker/res/layout/cat_picker_positive_button.xml new file mode 100644 index 00000000000..8d23592bb6d --- /dev/null +++ b/catalog/java/io/material/catalog/datepicker/res/layout/cat_picker_positive_button.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + diff --git a/catalog/java/io/material/catalog/datepicker/res/layout/picker_main_demo.xml b/catalog/java/io/material/catalog/datepicker/res/layout/picker_main_demo.xml index e204aefe9f5..515be51a1b6 100644 --- a/catalog/java/io/material/catalog/datepicker/res/layout/picker_main_demo.xml +++ b/catalog/java/io/material/catalog/datepicker/res/layout/picker_main_demo.xml @@ -39,6 +39,8 @@ + + diff --git a/catalog/java/io/material/catalog/datepicker/res/values/strings.xml b/catalog/java/io/material/catalog/datepicker/res/values/strings.xml index e2c2d839e24..69a2a6bd3f1 100644 --- a/catalog/java/io/material/catalog/datepicker/res/values/strings.xml +++ b/catalog/java/io/material/catalog/datepicker/res/values/strings.xml @@ -105,4 +105,22 @@ Text Input + + Picker Positive Button + + Default + + Custom + + + Picker Negative Button + + Default + + Custom + + + DONE + + Negative diff --git a/lib/java/com/google/android/material/datepicker/MaterialDatePicker.java b/lib/java/com/google/android/material/datepicker/MaterialDatePicker.java index fdb0fba05ac..de04bbdf013 100644 --- a/lib/java/com/google/android/material/datepicker/MaterialDatePicker.java +++ b/lib/java/com/google/android/material/datepicker/MaterialDatePicker.java @@ -66,6 +66,10 @@ public final class MaterialDatePicker extends DialogFragment { private static final String CALENDAR_CONSTRAINTS_KEY = "CALENDAR_CONSTRAINTS_KEY"; private static final String TITLE_TEXT_RES_ID_KEY = "TITLE_TEXT_RES_ID_KEY"; private static final String TITLE_TEXT_KEY = "TITLE_TEXT_KEY"; + private static final String POSITIVE_BUTTON_TEXT_RES_ID_KEY = "POSITIVE_BUTTON_TEXT_RES_ID_KEY"; + private static final String POSITIVE_BUTTON_TEXT_KEY = "POSITIVE_BUTTON_TEXT_KEY"; + private static final String NEGATIVE_BUTTON_TEXT_RES_ID_KEY = "NEGATIVE_BUTTON_TEXT_RES_ID_KEY"; + private static final String NEGATIVE_BUTTON_TEXT_KEY = "NEGATIVE_BUTTON_TEXT_KEY"; private static final String INPUT_MODE_KEY = "INPUT_MODE_KEY"; static final Object CONFIRM_BUTTON_TAG = "CONFIRM_BUTTON_TAG"; @@ -123,6 +127,10 @@ public String getHeaderText() { private CharSequence titleText; private boolean fullscreen; @InputMode private int inputMode; + @StringRes private int positiveButtonTextResId; + private CharSequence positiveButtonText; + @StringRes private int negativeButtonTextResId; + private CharSequence negativeButtonText; private TextView headerSelectionText; private CheckableImageButton headerToggleButton; @@ -139,6 +147,10 @@ static MaterialDatePicker newInstance(@NonNull Builder options) { args.putInt(TITLE_TEXT_RES_ID_KEY, options.titleTextResId); args.putCharSequence(TITLE_TEXT_KEY, options.titleText); args.putInt(INPUT_MODE_KEY, options.inputMode); + args.putInt(POSITIVE_BUTTON_TEXT_RES_ID_KEY, options.positiveButtonTextResId); + args.putCharSequence(POSITIVE_BUTTON_TEXT_KEY, options.positiveButtonText); + args.putInt(NEGATIVE_BUTTON_TEXT_RES_ID_KEY, options.negativeButtonTextResId); + args.putCharSequence(NEGATIVE_BUTTON_TEXT_KEY, options.negativeButtonText); materialDatePickerDialogFragment.setArguments(args); return materialDatePickerDialogFragment; } @@ -157,6 +169,10 @@ public final void onSaveInstanceState(@NonNull Bundle bundle) { bundle.putParcelable(CALENDAR_CONSTRAINTS_KEY, constraintsBuilder.build()); bundle.putInt(TITLE_TEXT_RES_ID_KEY, titleTextResId); bundle.putCharSequence(TITLE_TEXT_KEY, titleText); + bundle.putInt(POSITIVE_BUTTON_TEXT_RES_ID_KEY, positiveButtonTextResId); + bundle.putCharSequence(POSITIVE_BUTTON_TEXT_KEY, positiveButtonText); + bundle.putInt(NEGATIVE_BUTTON_TEXT_RES_ID_KEY, negativeButtonTextResId); + bundle.putCharSequence(NEGATIVE_BUTTON_TEXT_KEY, negativeButtonText); } @Override @@ -169,6 +185,10 @@ public final void onCreate(@Nullable Bundle bundle) { titleTextResId = activeBundle.getInt(TITLE_TEXT_RES_ID_KEY); titleText = activeBundle.getCharSequence(TITLE_TEXT_KEY); inputMode = activeBundle.getInt(INPUT_MODE_KEY); + positiveButtonTextResId = activeBundle.getInt(POSITIVE_BUTTON_TEXT_RES_ID_KEY); + positiveButtonText = activeBundle.getCharSequence(POSITIVE_BUTTON_TEXT_KEY); + negativeButtonTextResId = activeBundle.getInt(NEGATIVE_BUTTON_TEXT_RES_ID_KEY); + negativeButtonText = activeBundle.getCharSequence(NEGATIVE_BUTTON_TEXT_KEY); } private int getThemeResId(Context context) { @@ -238,6 +258,11 @@ public final View onCreateView( confirmButton.setEnabled(false); } confirmButton.setTag(CONFIRM_BUTTON_TAG); + if (positiveButtonText != null) { + confirmButton.setText(positiveButtonText); + } else if (positiveButtonTextResId != 0) { + confirmButton.setText(positiveButtonTextResId); + } confirmButton.setOnClickListener( new View.OnClickListener() { @Override @@ -252,6 +277,11 @@ public void onClick(View v) { Button cancelButton = root.findViewById(R.id.cancel_button); cancelButton.setTag(CANCEL_BUTTON_TAG); + if (negativeButtonText != null) { + cancelButton.setText(negativeButtonText); + } else if (negativeButtonTextResId != 0) { + cancelButton.setText(negativeButtonTextResId); + } cancelButton.setOnClickListener( new View.OnClickListener() { @Override @@ -528,6 +558,10 @@ public static final class Builder { CalendarConstraints calendarConstraints; int titleTextResId = 0; CharSequence titleText = null; + int positiveButtonTextResId = 0; + CharSequence positiveButtonText = null; + int negativeButtonTextResId = 0; + CharSequence negativeButtonText = null; @Nullable S selection = null; @InputMode int inputMode = INPUT_MODE_CALENDAR; @@ -606,6 +640,54 @@ public Builder setTitleText(@Nullable CharSequence charSequence) { return this; } + /** + * Sets the text used in the positive button + * + * @param textId resource id to be used as text in the positive button + */ + @NonNull + public Builder setPositiveButtonText(@StringRes int textId) { + this.positiveButtonTextResId = textId; + this.positiveButtonText = null; + return this; + } + + /** + * Sets the text used in the positive button + * + * @param text text used in the positive button + */ + @NonNull + public Builder setPositiveButtonText(@Nullable CharSequence text) { + this.positiveButtonText = text; + this.positiveButtonTextResId = 0; + return this; + } + + /** + * Sets the text used in the negative button + * + * @param textId resource id to be used as text in the negative button + */ + @NonNull + public Builder setNegativeButtonText(@StringRes int textId) { + this.negativeButtonTextResId = textId; + this.negativeButtonText = null; + return this; + } + + /** + * Sets the text used in the negative button + * + * @param text text used in the negative button + */ + @NonNull + public Builder setNegativeButtonText(@Nullable CharSequence text) { + this.negativeButtonText = text; + this.negativeButtonTextResId = 0; + return this; + } + /** Sets the input mode to start with. */ @NonNull public Builder setInputMode(@InputMode int inputMode) {