Skip to content

Commit 168c691

Browse files
drchenraajkumars
authored andcommitted
[TimePicker] Add ability to customize positive and negative button
Resolves #2275 PiperOrigin-RevId: 398010381
1 parent 32ce65a commit 168c691

File tree

1 file changed

+86
-10
lines changed

1 file changed

+86
-10
lines changed

lib/java/com/google/android/material/timepicker/MaterialTimePicker.java

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ public final class MaterialTimePicker extends DialogFragment {
7575
@DrawableRes private int keyboardIcon;
7676
@DrawableRes private int clockIcon;
7777

78-
private int titleResId = 0;
79-
private String titleText;
78+
@StringRes private int titleResId = 0;
79+
private CharSequence titleText;
80+
@StringRes private int positiveButtonTextResId = 0;
81+
private CharSequence positiveButtonText;
82+
@StringRes private int negativeButtonTextResId = 0;
83+
private CharSequence negativeButtonText;
8084

8185
/** Values supported for the input type of the dialog. */
8286
@IntDef({INPUT_MODE_CLOCK, INPUT_MODE_KEYBOARD})
@@ -90,6 +94,10 @@ public final class MaterialTimePicker extends DialogFragment {
9094
static final String INPUT_MODE_EXTRA = "TIME_PICKER_INPUT_MODE";
9195
static final String TITLE_RES_EXTRA = "TIME_PICKER_TITLE_RES";
9296
static final String TITLE_TEXT_EXTRA = "TIME_PICKER_TITLE_TEXT";
97+
static final String POSITIVE_BUTTON_TEXT_RES_EXTRA = "TIME_PICKER_POSITIVE_BUTTON_TEXT_RES";
98+
static final String POSITIVE_BUTTON_TEXT_EXTRA = "TIME_PICKER_POSITIVE_BUTTON_TEXT";
99+
static final String NEGATIVE_BUTTON_TEXT_RES_EXTRA = "TIME_PICKER_NEGATIVE_BUTTON_TEXT_RES";
100+
static final String NEGATIVE_BUTTON_TEXT_EXTRA = "TIME_PICKER_NEGATIVE_BUTTON_TEXT";
93101
static final String OVERRIDE_THEME_RES_ID = "TIME_PICKER_OVERRIDE_THEME_RES_ID";
94102

95103
private MaterialButton modeButton;
@@ -108,10 +116,18 @@ private static MaterialTimePicker newInstance(@NonNull Builder options) {
108116
args.putParcelable(TIME_MODEL_EXTRA, options.time);
109117
args.putInt(INPUT_MODE_EXTRA, options.inputMode);
110118
args.putInt(TITLE_RES_EXTRA, options.titleTextResId);
111-
args.putInt(OVERRIDE_THEME_RES_ID, options.overrideThemeResId);
112119
if (options.titleText != null) {
113-
args.putString(TITLE_TEXT_EXTRA, options.titleText.toString());
120+
args.putCharSequence(TITLE_TEXT_EXTRA, options.titleText);
121+
}
122+
args.putInt(POSITIVE_BUTTON_TEXT_RES_EXTRA, options.positiveButtonTextResId);
123+
if (options.positiveButtonText != null) {
124+
args.putCharSequence(POSITIVE_BUTTON_TEXT_EXTRA, options.positiveButtonText);
125+
}
126+
args.putInt(NEGATIVE_BUTTON_TEXT_RES_EXTRA, options.negativeButtonTextResId);
127+
if (options.negativeButtonText != null) {
128+
args.putCharSequence(NEGATIVE_BUTTON_TEXT_EXTRA, options.negativeButtonText);
114129
}
130+
args.putInt(OVERRIDE_THEME_RES_ID, options.overrideThemeResId);
115131

116132
fragment.setArguments(args);
117133
return fragment;
@@ -186,7 +202,11 @@ public void onSaveInstanceState(@NonNull Bundle bundle) {
186202
bundle.putParcelable(TIME_MODEL_EXTRA, time);
187203
bundle.putInt(INPUT_MODE_EXTRA, inputMode);
188204
bundle.putInt(TITLE_RES_EXTRA, titleResId);
189-
bundle.putString(TITLE_TEXT_EXTRA, titleText);
205+
bundle.putCharSequence(TITLE_TEXT_EXTRA, titleText);
206+
bundle.putInt(POSITIVE_BUTTON_TEXT_RES_EXTRA, positiveButtonTextResId);
207+
bundle.putCharSequence(POSITIVE_BUTTON_TEXT_EXTRA, positiveButtonText);
208+
bundle.putInt(NEGATIVE_BUTTON_TEXT_RES_EXTRA, negativeButtonTextResId);
209+
bundle.putCharSequence(NEGATIVE_BUTTON_TEXT_EXTRA, negativeButtonText);
190210
bundle.putInt(OVERRIDE_THEME_RES_ID, overrideThemeResId);
191211
}
192212

@@ -201,7 +221,11 @@ private void restoreState(@Nullable Bundle bundle) {
201221
}
202222
inputMode = bundle.getInt(INPUT_MODE_EXTRA, INPUT_MODE_CLOCK);
203223
titleResId = bundle.getInt(TITLE_RES_EXTRA, 0);
204-
titleText = bundle.getString(TITLE_TEXT_EXTRA);
224+
titleText = bundle.getCharSequence(TITLE_TEXT_EXTRA);
225+
positiveButtonTextResId = bundle.getInt(POSITIVE_BUTTON_TEXT_RES_EXTRA, 0);
226+
positiveButtonText = bundle.getCharSequence(POSITIVE_BUTTON_TEXT_EXTRA);
227+
negativeButtonTextResId = bundle.getInt(NEGATIVE_BUTTON_TEXT_RES_EXTRA, 0);
228+
negativeButtonText = bundle.getCharSequence(NEGATIVE_BUTTON_TEXT_EXTRA);
205229
overrideThemeResId = bundle.getInt(OVERRIDE_THEME_RES_ID, 0);
206230
}
207231

@@ -227,12 +251,10 @@ public void onDoubleTap() {
227251
modeButton = root.findViewById(R.id.material_timepicker_mode_button);
228252
TextView headerTitle = root.findViewById(R.id.header_title);
229253

230-
if (!TextUtils.isEmpty(titleText)) {
231-
headerTitle.setText(titleText);
232-
}
233-
234254
if (titleResId != 0) {
235255
headerTitle.setText(titleResId);
256+
} else if (!TextUtils.isEmpty(titleText)) {
257+
headerTitle.setText(titleText);
236258
}
237259

238260
updateInputMode(modeButton);
@@ -247,6 +269,11 @@ public void onClick(View v) {
247269
dismiss();
248270
}
249271
});
272+
if (positiveButtonTextResId != 0) {
273+
okButton.setText(positiveButtonTextResId);
274+
} else if (!TextUtils.isEmpty(positiveButtonText)) {
275+
okButton.setText(positiveButtonText);
276+
}
250277

251278
cancelButton = root.findViewById(R.id.material_timepicker_cancel_button);
252279
cancelButton.setOnClickListener(
@@ -259,6 +286,12 @@ public void onClick(View v) {
259286
dismiss();
260287
}
261288
});
289+
if (negativeButtonTextResId != 0) {
290+
cancelButton.setText(negativeButtonTextResId);
291+
} else if (!TextUtils.isEmpty(negativeButtonText)) {
292+
cancelButton.setText(negativeButtonText);
293+
}
294+
262295
updateCancelButtonVisibility();
263296

264297
modeButton.setOnClickListener(
@@ -476,8 +509,15 @@ public static final class Builder {
476509
private TimeModel time = new TimeModel();
477510

478511
private int inputMode;
512+
@StringRes
479513
private int titleTextResId = 0;
480514
private CharSequence titleText;
515+
@StringRes
516+
private int positiveButtonTextResId = 0;
517+
private CharSequence positiveButtonText;
518+
@StringRes
519+
private int negativeButtonTextResId = 0;
520+
private CharSequence negativeButtonText;
481521
private int overrideThemeResId = 0;
482522

483523
/** Sets the input mode with which to start the time picker. */
@@ -540,6 +580,42 @@ public Builder setTitleText(@Nullable CharSequence charSequence) {
540580
return this;
541581
}
542582

583+
/**
584+
* Sets the text used in the positive action button.
585+
*/
586+
@NonNull
587+
public Builder setPositiveButtonText(@StringRes int positiveButtonTextResId) {
588+
this.positiveButtonTextResId = positiveButtonTextResId;
589+
return this;
590+
}
591+
592+
/**
593+
* Sets the text used in the positive action button.
594+
*/
595+
@NonNull
596+
public Builder setPositiveButtonText(@Nullable CharSequence positiveButtonText) {
597+
this.positiveButtonText = positiveButtonText;
598+
return this;
599+
}
600+
601+
/**
602+
* Sets the text used in the negative action button.
603+
*/
604+
@NonNull
605+
public Builder setNegativeButtonText(@StringRes int negativeButtonTextResId) {
606+
this.negativeButtonTextResId = negativeButtonTextResId;
607+
return this;
608+
}
609+
610+
/**
611+
* Sets the text used in the negative action button.
612+
*/
613+
@NonNull
614+
public Builder setNegativeButtonText(@Nullable CharSequence negativeButtonText) {
615+
this.negativeButtonText = negativeButtonText;
616+
return this;
617+
}
618+
543619
/** Sets the theme for the time picker. */
544620
@NonNull
545621
public Builder setTheme(@StyleRes int themeResId) {

0 commit comments

Comments
 (0)