Skip to content

Commit 85a4405

Browse files
paulfthomasraajkumars
authored andcommitted
[MaterialTimePicker] Add setters for hour and minute
Resolves #2515 PiperOrigin-RevId: 428563049
1 parent 6bf92e1 commit 85a4405

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import androidx.annotation.RestrictTo;
5050
import androidx.annotation.StringRes;
5151
import androidx.annotation.StyleRes;
52+
import androidx.annotation.VisibleForTesting;
5253
import androidx.core.view.ViewCompat;
5354
import androidx.core.view.accessibility.AccessibilityEventCompat;
5455
import com.google.android.material.button.MaterialButton;
@@ -136,17 +137,34 @@ private static MaterialTimePicker newInstance(@NonNull Builder options) {
136137
return fragment;
137138
}
138139

139-
@IntRange(from = 0, to = 60)
140+
/** Returns the minute in the range [0, 59]. */
141+
@IntRange(from = 0, to = 59)
140142
public int getMinute() {
141143
return time.minute;
142144
}
143145

146+
/** Sets the minute in the range [0, 59]. */
147+
public void setMinute(@IntRange(from = 0, to = 59) int minute) {
148+
time.setMinute(minute);
149+
if (activePresenter != null) {
150+
activePresenter.invalidate();
151+
}
152+
}
153+
144154
/** Returns the hour of day in the range [0, 23]. */
145155
@IntRange(from = 0, to = 23)
146156
public int getHour() {
147157
return time.hour % 24;
148158
}
149159

160+
/** Sets the hour of day in the range [0, 23]. */
161+
public void setHour(@IntRange(from = 0, to = 23) int hour) {
162+
time.setHour(hour);
163+
if (activePresenter != null) {
164+
activePresenter.invalidate();
165+
}
166+
}
167+
150168
@InputMode
151169
public int getInputMode() {
152170
return inputMode;
@@ -408,6 +426,11 @@ TimePickerClockPresenter getTimePickerClockPresenter() {
408426
return timePickerClockPresenter;
409427
}
410428

429+
@VisibleForTesting
430+
void setActivePresenter(@Nullable TimePickerPresenter presenter) {
431+
activePresenter = presenter;
432+
}
433+
411434
/** The supplied listener is called when the user confirms a valid selection. */
412435
public boolean addOnPositiveButtonClickListener(@NonNull OnClickListener listener) {
413436
return positiveButtonListeners.add(listener);
@@ -548,7 +571,7 @@ public Builder setHour(@IntRange(from = 0, to = 23) int hour) {
548571

549572
/** Sets the minute with which to start the time picker. */
550573
@NonNull
551-
public Builder setMinute(@IntRange(from = 0, to = 60) int minute) {
574+
public Builder setMinute(@IntRange(from = 0, to = 59) int minute) {
552575
time.setMinute(minute);
553576
return this;
554577
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void setHour(int hour) {
9191
this.hour = hour % 12 + (period == PM ? 12 : 0);
9292
}
9393

94-
public void setMinute(@IntRange(from = 0, to = 60) int minute) {
94+
public void setMinute(@IntRange(from = 0, to = 59) int minute) {
9595
this.minute = minute % 60;
9696
}
9797

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
interface TimePickerPresenter {
2020

21-
/** Do any final intialization */
21+
/** Do any final initialization */
2222
void initialize();
2323

2424
/** Refresh the data in the view based on the model */

0 commit comments

Comments
 (0)