Skip to content

Commit

Permalink
[MaterialDatePicker] added methods to use custom text for positive an…
Browse files Browse the repository at this point in the history
…d negative button

Resolves #1607

GIT_ORIGIN_REV_ID=75093200ddeae117ae57a3e89e28d109f520ec8d
PiperOrigin-RevId: 407376627
  • Loading branch information
gabrielemariotti authored and drchen committed Nov 4, 2021
1 parent aaac96d commit b2fbace
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 0 deletions.
Expand Up @@ -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 -> {
Expand All @@ -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);
Expand All @@ -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();
Expand Down
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Copyright 2019 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
https://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.
-->

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cat_picker_negative_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/cat_picker_demo_padding"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cat_picker_negative_button"
android:textAppearance="?attr/textAppearanceTitleLarge"/>
<RadioGroup
android:id="@+id/cat_picker_negative_button_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/cat_picker_negative_button_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/cat_picker_negative_button_default"
android:textAppearance="?attr/textAppearanceBodyLarge"/>
<RadioButton
android:id="@+id/cat_picker_negative_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/cat_picker_demo_padding"
android:layout_marginLeft="@dimen/cat_picker_demo_padding"
android:text="@string/cat_picker_negative_button_custom"
android:textAppearance="?attr/textAppearanceBodyLarge"/>

</RadioGroup>
</LinearLayout>
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Copyright 2019 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
https://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.
-->

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cat_picker_positive_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/cat_picker_demo_padding"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cat_picker_positive_button"
android:textAppearance="?attr/textAppearanceTitleLarge"/>
<RadioGroup
android:id="@+id/cat_picker_positive_button_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/cat_picker_positive_button_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/cat_picker_positive_button_default"
android:textAppearance="?attr/textAppearanceBodyLarge"/>
<RadioButton
android:id="@+id/cat_picker_positive_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/cat_picker_demo_padding"
android:layout_marginLeft="@dimen/cat_picker_demo_padding"
android:text="@string/cat_picker_positive_button_custom"
android:textAppearance="?attr/textAppearanceBodyLarge"/>

</RadioGroup>
</LinearLayout>
Expand Up @@ -39,6 +39,8 @@
<include layout="@layout/cat_picker_bounds"/>
<include layout="@layout/cat_picker_validation"/>
<include layout="@layout/cat_picker_title"/>
<include layout="@layout/cat_picker_positive_button"/>
<include layout="@layout/cat_picker_negative_button"/>
<include layout="@layout/cat_picker_opening_month"/>
<include layout="@layout/cat_picker_selection"/>
<include layout="@layout/cat_picker_input_mode"/>
Expand Down
18 changes: 18 additions & 0 deletions catalog/java/io/material/catalog/datepicker/res/values/strings.xml
Expand Up @@ -105,4 +105,22 @@
<!-- Indicates that the picker will start with text input mode [CHAR LIMIT=25] -->
<string name="cat_picker_input_mode_text">Text Input</string>

<!-- Grouping label for a set of radio buttons that choose the positive button [CHAR LIMIT=50] -->
<string name="cat_picker_positive_button">Picker Positive Button</string>
<!-- Indicates that the default title will be used [CHAR LIMIT=25] -->
<string name="cat_picker_positive_button_default">Default</string>
<!-- Indicates that a custom title will be used [CHAR LIMIT=25] -->
<string name="cat_picker_positive_button_custom">Custom</string>

<!-- Grouping label for a set of radio buttons that choose the negative button [CHAR LIMIT=50] -->
<string name="cat_picker_negative_button">Picker Negative Button</string>
<!-- Indicates that the default title will be used [CHAR LIMIT=25] -->
<string name="cat_picker_negative_button_default">Default</string>
<!-- Indicates that a custom title will be used [CHAR LIMIT=25] -->
<string name="cat_picker_negative_button_custom">Custom</string>

<!-- The custom positive button text -->
<string name="cat_picker_positive_button_text">DONE</string>
<!-- The custom negative button text -->
<string name="cat_picker_negative_button_text">Negative</string>
</resources>
Expand Up @@ -66,6 +66,10 @@ public final class MaterialDatePicker<S> 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";
Expand Down Expand Up @@ -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;
Expand All @@ -139,6 +147,10 @@ static <S> MaterialDatePicker<S> newInstance(@NonNull Builder<S> 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;
}
Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -528,6 +558,10 @@ public static final class Builder<S> {
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;

Expand Down Expand Up @@ -606,6 +640,54 @@ public Builder<S> 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<S> 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<S> 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<S> 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<S> setNegativeButtonText(@Nullable CharSequence text) {
this.negativeButtonText = text;
this.negativeButtonTextResId = 0;
return this;
}

/** Sets the input mode to start with. */
@NonNull
public Builder<S> setInputMode(@InputMode int inputMode) {
Expand Down

0 comments on commit b2fbace

Please sign in to comment.