-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathMaterialTimePicker.java
685 lines (595 loc) · 23.1 KB
/
MaterialTimePicker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/*
* Copyright 2020 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.
*/
package com.google.android.material.timepicker;
import com.google.android.material.R;
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
import static com.google.android.material.timepicker.TimeFormat.CLOCK_24H;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
import android.text.TextUtils;
import android.util.Pair;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.annotation.VisibleForTesting;
import androidx.core.view.accessibility.AccessibilityEventCompat;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.resources.MaterialAttributes;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.android.material.timepicker.TimePickerView.OnDoubleTapListener;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* A {@link Dialog} with a clock display and a clock face to choose the time.
*
* <p>For more information, see the <a
* href="https://github.com/material-components/material-components-android/blob/master/docs/components/TimePicker.md">component
* developer guidance</a> and <a href="https://material.io/components/time-pickers/overview">design
* guidelines</a>.
*/
public final class MaterialTimePicker extends DialogFragment implements OnDoubleTapListener {
private final Set<OnClickListener> positiveButtonListeners = new LinkedHashSet<>();
private final Set<OnClickListener> negativeButtonListeners = new LinkedHashSet<>();
private final Set<OnCancelListener> cancelListeners = new LinkedHashSet<>();
private final Set<OnDismissListener> dismissListeners = new LinkedHashSet<>();
private TimePickerView timePickerView;
private ViewStub textInputStub;
@Nullable private TimePickerClockPresenter timePickerClockPresenter;
@Nullable private TimePickerTextInputPresenter timePickerTextInputPresenter;
@Nullable private TimePickerPresenter activePresenter;
@DrawableRes private int keyboardIcon;
@DrawableRes private int clockIcon;
@StringRes private int titleResId = 0;
private CharSequence titleText;
@StringRes private int positiveButtonTextResId = 0;
private CharSequence positiveButtonText;
@StringRes private int negativeButtonTextResId = 0;
private CharSequence negativeButtonText;
/** Values supported for the input type of the dialog. */
@IntDef({INPUT_MODE_CLOCK, INPUT_MODE_KEYBOARD})
@Retention(RetentionPolicy.SOURCE)
@interface InputMode {}
public static final int INPUT_MODE_CLOCK = 0;
public static final int INPUT_MODE_KEYBOARD = 1;
static final String TIME_MODEL_EXTRA = "TIME_PICKER_TIME_MODEL";
static final String INPUT_MODE_EXTRA = "TIME_PICKER_INPUT_MODE";
static final String TITLE_RES_EXTRA = "TIME_PICKER_TITLE_RES";
static final String TITLE_TEXT_EXTRA = "TIME_PICKER_TITLE_TEXT";
static final String POSITIVE_BUTTON_TEXT_RES_EXTRA = "TIME_PICKER_POSITIVE_BUTTON_TEXT_RES";
static final String POSITIVE_BUTTON_TEXT_EXTRA = "TIME_PICKER_POSITIVE_BUTTON_TEXT";
static final String NEGATIVE_BUTTON_TEXT_RES_EXTRA = "TIME_PICKER_NEGATIVE_BUTTON_TEXT_RES";
static final String NEGATIVE_BUTTON_TEXT_EXTRA = "TIME_PICKER_NEGATIVE_BUTTON_TEXT";
static final String OVERRIDE_THEME_RES_ID = "TIME_PICKER_OVERRIDE_THEME_RES_ID";
private MaterialButton modeButton;
private Button cancelButton;
@InputMode private int inputMode = INPUT_MODE_CLOCK;
private TimeModel time;
private int overrideThemeResId = 0;
@NonNull
private static MaterialTimePicker newInstance(@NonNull Builder options) {
MaterialTimePicker fragment = new MaterialTimePicker();
Bundle args = new Bundle();
args.putParcelable(TIME_MODEL_EXTRA, options.time);
if (options.inputMode != null) {
args.putInt(INPUT_MODE_EXTRA, options.inputMode);
}
args.putInt(TITLE_RES_EXTRA, options.titleTextResId);
if (options.titleText != null) {
args.putCharSequence(TITLE_TEXT_EXTRA, options.titleText);
}
args.putInt(POSITIVE_BUTTON_TEXT_RES_EXTRA, options.positiveButtonTextResId);
if (options.positiveButtonText != null) {
args.putCharSequence(POSITIVE_BUTTON_TEXT_EXTRA, options.positiveButtonText);
}
args.putInt(NEGATIVE_BUTTON_TEXT_RES_EXTRA, options.negativeButtonTextResId);
if (options.negativeButtonText != null) {
args.putCharSequence(NEGATIVE_BUTTON_TEXT_EXTRA, options.negativeButtonText);
}
args.putInt(OVERRIDE_THEME_RES_ID, options.overrideThemeResId);
fragment.setArguments(args);
return fragment;
}
/** Returns the minute in the range [0, 59]. */
@IntRange(from = 0, to = 59)
public int getMinute() {
return time.minute;
}
/** Sets the minute in the range [0, 59]. */
public void setMinute(@IntRange(from = 0, to = 59) int minute) {
time.setMinute(minute);
if (activePresenter != null) {
activePresenter.invalidate();
}
}
/** Returns the hour of day in the range [0, 23]. */
@IntRange(from = 0, to = 23)
public int getHour() {
return time.hour % 24;
}
/** Sets the hour of day in the range [0, 23]. */
public void setHour(@IntRange(from = 0, to = 23) int hour) {
time.setHour(hour);
if (activePresenter != null) {
activePresenter.invalidate();
}
}
@InputMode
public int getInputMode() {
return inputMode;
}
@NonNull
@Override
public final Dialog onCreateDialog(@Nullable Bundle bundle) {
Dialog dialog = new Dialog(requireContext(), getThemeResId());
Context context = dialog.getContext();
MaterialShapeDrawable background =
new MaterialShapeDrawable(
context,
null,
R.attr.materialTimePickerStyle,
R.style.Widget_MaterialComponents_TimePicker);
TypedArray a =
context.obtainStyledAttributes(
null,
R.styleable.MaterialTimePicker,
R.attr.materialTimePickerStyle,
R.style.Widget_MaterialComponents_TimePicker);
clockIcon = a.getResourceId(R.styleable.MaterialTimePicker_clockIcon, 0);
keyboardIcon = a.getResourceId(R.styleable.MaterialTimePicker_keyboardIcon, 0);
int backgroundColor = a.getColor(R.styleable.MaterialTimePicker_backgroundTint, 0);
a.recycle();
background.initializeElevationOverlay(context);
background.setFillColor(ColorStateList.valueOf(backgroundColor));
Window window = dialog.getWindow();
window.setBackgroundDrawable(background);
window.requestFeature(Window.FEATURE_NO_TITLE);
// On some Android APIs the dialog won't wrap content by default. Explicitly update here.
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// This has to be done after requestFeature() is called on API <= 23.
background.setElevation(window.getDecorView().getElevation());
return dialog;
}
@Override
public void onCreate(@Nullable Bundle bundle) {
super.onCreate(bundle);
restoreState(bundle == null ? getArguments() : bundle);
}
@Override
public void onSaveInstanceState(@NonNull Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putParcelable(TIME_MODEL_EXTRA, time);
bundle.putInt(INPUT_MODE_EXTRA, inputMode);
bundle.putInt(TITLE_RES_EXTRA, titleResId);
bundle.putCharSequence(TITLE_TEXT_EXTRA, titleText);
bundle.putInt(POSITIVE_BUTTON_TEXT_RES_EXTRA, positiveButtonTextResId);
bundle.putCharSequence(POSITIVE_BUTTON_TEXT_EXTRA, positiveButtonText);
bundle.putInt(NEGATIVE_BUTTON_TEXT_RES_EXTRA, negativeButtonTextResId);
bundle.putCharSequence(NEGATIVE_BUTTON_TEXT_EXTRA, negativeButtonText);
bundle.putInt(OVERRIDE_THEME_RES_ID, overrideThemeResId);
}
private void restoreState(@Nullable Bundle bundle) {
if (bundle == null) {
return;
}
time = bundle.getParcelable(TIME_MODEL_EXTRA);
if (time == null) {
time = new TimeModel();
}
int defaultInputMode = time.format == CLOCK_24H ? INPUT_MODE_KEYBOARD : INPUT_MODE_CLOCK;
inputMode = bundle.getInt(INPUT_MODE_EXTRA, defaultInputMode);
titleResId = bundle.getInt(TITLE_RES_EXTRA, 0);
titleText = bundle.getCharSequence(TITLE_TEXT_EXTRA);
positiveButtonTextResId = bundle.getInt(POSITIVE_BUTTON_TEXT_RES_EXTRA, 0);
positiveButtonText = bundle.getCharSequence(POSITIVE_BUTTON_TEXT_EXTRA);
negativeButtonTextResId = bundle.getInt(NEGATIVE_BUTTON_TEXT_RES_EXTRA, 0);
negativeButtonText = bundle.getCharSequence(NEGATIVE_BUTTON_TEXT_EXTRA);
overrideThemeResId = bundle.getInt(OVERRIDE_THEME_RES_ID, 0);
}
@NonNull
@Override
public final View onCreateView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle) {
ViewGroup root =
(ViewGroup) layoutInflater.inflate(R.layout.material_timepicker_dialog, viewGroup);
timePickerView = root.findViewById(R.id.material_timepicker_view);
timePickerView.setOnDoubleTapListener(this);
textInputStub = root.findViewById(R.id.material_textinput_timepicker);
modeButton = root.findViewById(R.id.material_timepicker_mode_button);
TextView headerTitle = root.findViewById(R.id.header_title);
if (titleResId != 0) {
headerTitle.setText(titleResId);
} else if (!TextUtils.isEmpty(titleText)) {
headerTitle.setText(titleText);
}
updateInputMode(modeButton);
Button okButton = root.findViewById(R.id.material_timepicker_ok_button);
okButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
for (OnClickListener listener : positiveButtonListeners) {
listener.onClick(v);
}
dismiss();
}
});
if (positiveButtonTextResId != 0) {
okButton.setText(positiveButtonTextResId);
} else if (!TextUtils.isEmpty(positiveButtonText)) {
okButton.setText(positiveButtonText);
}
cancelButton = root.findViewById(R.id.material_timepicker_cancel_button);
cancelButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
for (OnClickListener listener : negativeButtonListeners) {
listener.onClick(v);
}
dismiss();
}
});
if (negativeButtonTextResId != 0) {
cancelButton.setText(negativeButtonTextResId);
} else if (!TextUtils.isEmpty(negativeButtonText)) {
cancelButton.setText(negativeButtonText);
}
updateCancelButtonVisibility();
modeButton.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
inputMode = (inputMode == INPUT_MODE_CLOCK) ? INPUT_MODE_KEYBOARD : INPUT_MODE_CLOCK;
updateInputMode(modeButton);
}
});
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
super.onViewCreated(view, bundle);
// TODO(b/246354286): Investigate issue with keyboard not showing on Android 12+
if (activePresenter instanceof TimePickerTextInputPresenter) {
view.postDelayed(
() -> {
if (activePresenter instanceof TimePickerTextInputPresenter) {
((TimePickerTextInputPresenter) activePresenter).resetChecked();
}
},
100);
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
activePresenter = null;
timePickerClockPresenter = null;
timePickerTextInputPresenter = null;
if (timePickerView != null) {
timePickerView.setOnDoubleTapListener(null);
timePickerView = null;
}
}
@Override
public final void onCancel(@NonNull DialogInterface dialogInterface) {
for (OnCancelListener listener : cancelListeners) {
listener.onCancel(dialogInterface);
}
super.onCancel(dialogInterface);
}
@Override
public final void onDismiss(@NonNull DialogInterface dialogInterface) {
for (OnDismissListener listener : dismissListeners) {
listener.onDismiss(dialogInterface);
}
super.onDismiss(dialogInterface);
}
@Override
public void setCancelable(boolean cancelable) {
super.setCancelable(cancelable);
updateCancelButtonVisibility();
}
/** @hide */
@RestrictTo(LIBRARY_GROUP)
@Override
public void onDoubleTap() {
inputMode = INPUT_MODE_KEYBOARD;
updateInputMode(modeButton);
timePickerTextInputPresenter.resetChecked();
}
private void updateInputMode(MaterialButton modeButton) {
if (modeButton == null || timePickerView == null || textInputStub == null) {
return;
}
if (activePresenter != null) {
activePresenter.hide();
}
activePresenter =
initializeOrRetrieveActivePresenterForMode(inputMode, timePickerView, textInputStub);
activePresenter.show();
activePresenter.invalidate();
Pair<Integer, Integer> buttonData = dataForMode(inputMode);
modeButton.setIconResource(buttonData.first);
modeButton.setContentDescription(getResources().getString(buttonData.second));
modeButton.sendAccessibilityEvent(
AccessibilityEventCompat.CONTENT_CHANGE_TYPE_CONTENT_DESCRIPTION);
}
private void updateCancelButtonVisibility() {
if (cancelButton != null) {
cancelButton.setVisibility(isCancelable() ? View.VISIBLE : View.GONE);
}
}
private TimePickerPresenter initializeOrRetrieveActivePresenterForMode(
int mode, @NonNull TimePickerView timePickerView, @NonNull ViewStub textInputStub) {
if (mode == INPUT_MODE_CLOCK) {
timePickerClockPresenter =
timePickerClockPresenter == null
? new TimePickerClockPresenter(timePickerView, time)
: timePickerClockPresenter;
return timePickerClockPresenter;
}
if (timePickerTextInputPresenter == null) {
LinearLayout textInputView = (LinearLayout) textInputStub.inflate();
timePickerTextInputPresenter = new TimePickerTextInputPresenter(textInputView, time);
}
timePickerTextInputPresenter.clearCheck();
return timePickerTextInputPresenter;
}
private Pair<Integer, Integer> dataForMode(@InputMode int mode) {
switch (mode) {
case INPUT_MODE_KEYBOARD:
return new Pair<>(clockIcon, R.string.material_timepicker_clock_mode_description);
case INPUT_MODE_CLOCK:
return new Pair<>(keyboardIcon, R.string.material_timepicker_text_input_mode_description);
}
throw new IllegalArgumentException("no icon for mode: " + mode);
}
@Nullable
TimePickerClockPresenter getTimePickerClockPresenter() {
return timePickerClockPresenter;
}
@VisibleForTesting
void setActivePresenter(@Nullable TimePickerPresenter presenter) {
activePresenter = presenter;
}
/** The supplied listener is called when the user confirms a valid selection. */
public boolean addOnPositiveButtonClickListener(@NonNull OnClickListener listener) {
return positiveButtonListeners.add(listener);
}
/**
* Removes a listener previously added via {@link
* MaterialTimePicker#addOnPositiveButtonClickListener(OnClickListener)}.
*/
public boolean removeOnPositiveButtonClickListener(@NonNull OnClickListener listener) {
return positiveButtonListeners.remove(listener);
}
/**
* Removes all listeners added via {@link
* MaterialTimePicker#addOnPositiveButtonClickListener(OnClickListener)}.
*/
public void clearOnPositiveButtonClickListeners() {
positiveButtonListeners.clear();
}
/** The supplied listener is called when the user clicks the cancel button. */
public boolean addOnNegativeButtonClickListener(@NonNull OnClickListener listener) {
return negativeButtonListeners.add(listener);
}
/**
* Removes a listener previously added via {@link
* MaterialTimePicker#addOnNegativeButtonClickListener(OnClickListener)}.
*/
public boolean removeOnNegativeButtonClickListener(@NonNull OnClickListener listener) {
return negativeButtonListeners.remove(listener);
}
/**
* Removes all listeners added via {@link
* MaterialTimePicker#addOnNegativeButtonClickListener(OnClickListener)}.
*/
public void clearOnNegativeButtonClickListeners() {
negativeButtonListeners.clear();
}
/**
* The supplied listener is called when the user cancels the picker via back button or a touch
* outside the view.
*
* <p>It is not called when the user clicks the cancel button. To add a listener for use when the
* user clicks the cancel button, use {@link
* MaterialTimePicker#addOnNegativeButtonClickListener(OnClickListener)}.
*/
public boolean addOnCancelListener(@NonNull OnCancelListener listener) {
return cancelListeners.add(listener);
}
/**
* Removes a listener previously added via {@link
* MaterialTimePicker#addOnCancelListener(OnCancelListener)}.
*/
public boolean removeOnCancelListener(@NonNull OnCancelListener listener) {
return cancelListeners.remove(listener);
}
/**
* Removes all listeners added via {@link
* MaterialTimePicker#addOnCancelListener(OnCancelListener)}.
*/
public void clearOnCancelListeners() {
cancelListeners.clear();
}
/**
* The supplied listener is called whenever the DialogFragment is dismissed, no matter how it is
* dismissed.
*/
public boolean addOnDismissListener(@NonNull OnDismissListener listener) {
return dismissListeners.add(listener);
}
/**
* Removes a listener previously added via {@link
* MaterialTimePicker#addOnDismissListener(OnDismissListener)}.
*/
public boolean removeOnDismissListener(@NonNull OnDismissListener listener) {
return dismissListeners.remove(listener);
}
/**
* Removes all listeners added via {@link
* MaterialTimePicker#addOnDismissListener(OnDismissListener)}.
*/
public void clearOnDismissListeners() {
dismissListeners.clear();
}
private int getThemeResId() {
if (overrideThemeResId != 0) {
return overrideThemeResId;
}
TypedValue value = MaterialAttributes.resolve(requireContext(), R.attr.materialTimePickerTheme);
return value == null ? 0 : value.data;
}
/** Used to create {@link MaterialTimePicker} instances. */
public static final class Builder {
private TimeModel time = new TimeModel();
@Nullable private Integer inputMode;
@StringRes
private int titleTextResId = 0;
private CharSequence titleText;
@StringRes
private int positiveButtonTextResId = 0;
private CharSequence positiveButtonText;
@StringRes
private int negativeButtonTextResId = 0;
private CharSequence negativeButtonText;
private int overrideThemeResId = 0;
/** Sets the input mode with which to start the time picker. */
@NonNull
@CanIgnoreReturnValue
public Builder setInputMode(@InputMode int inputMode) {
this.inputMode = inputMode;
return this;
}
/**
* Sets the hour with which to start the time picker.
*
* @param hour The hour value is independent of the time format ({@link #setTimeFormat(int)}),
* and should always be a number in the [0, 23] range.
*/
@NonNull
@CanIgnoreReturnValue
public Builder setHour(@IntRange(from = 0, to = 23) int hour) {
time.setHourOfDay(hour);
return this;
}
/** Sets the minute with which to start the time picker. */
@NonNull
@CanIgnoreReturnValue
public Builder setMinute(@IntRange(from = 0, to = 59) int minute) {
time.setMinute(minute);
return this;
}
/**
* Sets the time format for the time picker.
*
* @param format Either {@code CLOCK_12H} 12 hour format with an AM/PM toggle or {@code
* CLOCK_24} 24 hour format without toggle.
*/
@NonNull
@CanIgnoreReturnValue
public Builder setTimeFormat(@TimeFormat int format) {
int hour = time.hour;
int minute = time.minute;
time = new TimeModel(format);
time.setMinute(minute);
time.setHourOfDay(hour);
return this;
}
/** Sets the text used to guide the user at the top of the picker. */
@NonNull
@CanIgnoreReturnValue
public Builder setTitleText(@StringRes int titleTextResId) {
this.titleTextResId = titleTextResId;
return this;
}
/** Sets the text used to guide the user at the top of the picker. */
@NonNull
@CanIgnoreReturnValue
public Builder setTitleText(@Nullable CharSequence charSequence) {
this.titleText = charSequence;
return this;
}
/** Sets the text used in the positive action button. */
@NonNull
@CanIgnoreReturnValue
public Builder setPositiveButtonText(@StringRes int positiveButtonTextResId) {
this.positiveButtonTextResId = positiveButtonTextResId;
return this;
}
/** Sets the text used in the positive action button. */
@NonNull
@CanIgnoreReturnValue
public Builder setPositiveButtonText(@Nullable CharSequence positiveButtonText) {
this.positiveButtonText = positiveButtonText;
return this;
}
/** Sets the text used in the negative action button. */
@NonNull
@CanIgnoreReturnValue
public Builder setNegativeButtonText(@StringRes int negativeButtonTextResId) {
this.negativeButtonTextResId = negativeButtonTextResId;
return this;
}
/** Sets the text used in the negative action button. */
@NonNull
@CanIgnoreReturnValue
public Builder setNegativeButtonText(@Nullable CharSequence negativeButtonText) {
this.negativeButtonText = negativeButtonText;
return this;
}
/** Sets the theme for the time picker. */
@NonNull
@CanIgnoreReturnValue
public Builder setTheme(@StyleRes int themeResId) {
this.overrideThemeResId = themeResId;
return this;
}
/** Creates a {@link MaterialTimePicker} with the provided options. */
@NonNull
public MaterialTimePicker build() {
return MaterialTimePicker.newInstance(this);
}
}
}