Skip to content

Commit 09f1ee5

Browse files
pubiqqafohrman
authored andcommitted
[Catalog][TimePicker] Add input mode selector
Resolves #2902 GIT_ORIGIN_REV_ID=3d62554886116fba051d8030a731678f3d181d30 Co-authored-by: paulfthomas PiperOrigin-RevId: 474614468
1 parent ab063b7 commit 09f1ee5

File tree

3 files changed

+161
-25
lines changed

3 files changed

+161
-25
lines changed

catalog/java/io/material/catalog/timepicker/TimePickerMainDemoFragment.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717

1818
import io.material.catalog.R;
1919

20+
import static com.google.android.material.timepicker.MaterialTimePicker.INPUT_MODE_CLOCK;
21+
import static com.google.android.material.timepicker.MaterialTimePicker.INPUT_MODE_KEYBOARD;
22+
import static com.google.android.material.timepicker.TimeFormat.CLOCK_12H;
23+
import static com.google.android.material.timepicker.TimeFormat.CLOCK_24H;
24+
2025
import android.os.Bundle;
2126
import androidx.appcompat.widget.SwitchCompat;
2227
import android.text.format.DateFormat;
28+
import android.util.Log;
2329
import android.view.LayoutInflater;
2430
import android.view.View;
2531
import android.view.ViewGroup;
@@ -37,11 +43,14 @@
3743
/** A fragment that displays the main Picker demos for the Catalog app. */
3844
public class TimePickerMainDemoFragment extends DemoFragment {
3945

46+
private static final String TAG = TimePickerMainDemoFragment.class.getSimpleName();
47+
4048
private int hour;
4149
private int minute;
4250

4351
private final SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a", Locale.getDefault());
4452
@TimeFormat private int clockFormat;
53+
@Nullable private Integer timeInputMode;
4554
private TextView textView;
4655

4756
@Override
@@ -51,37 +60,70 @@ public View onCreateDemoView(
5160
ViewGroup view =
5261
(ViewGroup) layoutInflater.inflate(R.layout.time_picker_main_demo, viewGroup, false);
5362

54-
Button button = view.findViewById(R.id.timepicker_button);
5563
textView = view.findViewById(R.id.timepicker_time);
56-
MaterialButtonToggleGroup timeFormatToggle = view.findViewById(R.id.time_format_toggle);
57-
clockFormat = TimeFormat.CLOCK_12H;
58-
timeFormatToggle.check(R.id.time_format_12h);
5964

65+
MaterialButtonToggleGroup timeFormatToggle = view.findViewById(R.id.time_format_toggle);
6066
timeFormatToggle.addOnButtonCheckedListener(
6167
(group, checkedId, isChecked) -> {
6268
if (isChecked) {
63-
boolean isSystem24Hour = DateFormat.is24HourFormat(getContext());
64-
boolean is24Hour =
65-
checkedId == R.id.time_format_24h
66-
|| (checkedId == R.id.time_format_system && isSystem24Hour);
69+
if (checkedId == R.id.time_format_12h) {
70+
clockFormat = CLOCK_12H;
71+
} else if (checkedId == R.id.time_format_24h) {
72+
clockFormat = CLOCK_24H;
73+
} else if (checkedId == R.id.time_format_system) {
74+
boolean isSystem24Hour = DateFormat.is24HourFormat(getContext());
75+
clockFormat = isSystem24Hour ? CLOCK_24H : CLOCK_12H;
76+
} else {
77+
Log.d(TAG, "Invalid time format selection: " + checkedId);
78+
}
79+
}
80+
});
6781

68-
clockFormat = is24Hour ? TimeFormat.CLOCK_24H : TimeFormat.CLOCK_12H;
82+
MaterialButtonToggleGroup timeInputModeToggle = view.findViewById(R.id.time_input_mode_toggle);
83+
timeInputModeToggle.addOnButtonCheckedListener(
84+
(group, checkedId, isChecked) -> {
85+
if (isChecked) {
86+
if (checkedId == R.id.time_input_mode_clock) {
87+
timeInputMode = INPUT_MODE_CLOCK;
88+
} else if (checkedId == R.id.time_input_mode_keyboard) {
89+
timeInputMode = INPUT_MODE_KEYBOARD;
90+
} else if (checkedId == R.id.time_input_mode_default) {
91+
timeInputMode = null;
92+
} else {
93+
Log.d(TAG, "Invalid time input mode selection: " + checkedId);
94+
}
6995
}
7096
});
7197

7298
SwitchCompat frameworkSwitch = view.findViewById(R.id.framework_switch);
99+
frameworkSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
100+
for (int i = 0; i < timeInputModeToggle.getChildCount(); i++) {
101+
View child = timeInputModeToggle.getChildAt(i);
102+
child.setEnabled(!isChecked);
103+
}
104+
});
105+
106+
timeFormatToggle.check(R.id.time_format_system);
107+
timeInputModeToggle.check(R.id.time_input_mode_default);
108+
frameworkSwitch.setChecked(false);
109+
110+
Button button = view.findViewById(R.id.timepicker_button);
73111
button.setOnClickListener(v -> {
74112
if (frameworkSwitch.isChecked()) {
75113
showFrameworkTimepicker();
76114
return;
77115
}
78116

79-
MaterialTimePicker materialTimePicker = new MaterialTimePicker.Builder()
117+
MaterialTimePicker.Builder materialTimePickerBuilder = new MaterialTimePicker.Builder()
80118
.setTimeFormat(clockFormat)
81119
.setHour(hour)
82-
.setMinute(minute)
83-
.build();
120+
.setMinute(minute);
121+
122+
if (timeInputMode != null) {
123+
materialTimePickerBuilder.setInputMode(timeInputMode);
124+
}
84125

126+
MaterialTimePicker materialTimePicker = materialTimePickerBuilder.build();
85127
materialTimePicker.show(requireFragmentManager(), "fragment_tag");
86128

87129
materialTimePicker.addOnPositiveButtonClickListener(dialog -> {
@@ -102,7 +144,7 @@ private void showFrameworkTimepicker() {
102144
TimePickerMainDemoFragment.this.onTimeSet(hourOfDay, minute),
103145
hour,
104146
minute,
105-
clockFormat == TimeFormat.CLOCK_24H);
147+
clockFormat == CLOCK_24H);
106148
timePickerDialog.show();
107149
}
108150

catalog/java/io/material/catalog/timepicker/res/layout/time_picker_main_demo.xml

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,41 @@
4646
app:layout_constraintStart_toEndOf="@id/timepicker_button"
4747
app:layout_constraintTop_toTopOf="@id/timepicker_button" />
4848

49+
<TextView
50+
android:id="@+id/time_format_label"
51+
android:layout_width="0dp"
52+
android:layout_height="wrap_content"
53+
android:layout_marginStart="16dp"
54+
android:layout_marginLeft="16dp"
55+
android:layout_marginTop="16dp"
56+
android:layout_marginEnd="16dp"
57+
android:layout_marginRight="16dp"
58+
android:text="@string/time_format_label"
59+
android:textAlignment="center"
60+
android:textAppearance="?attr/textAppearanceBodyLarge"
61+
app:layout_constraintEnd_toEndOf="parent"
62+
app:layout_constraintStart_toStartOf="parent"
63+
app:layout_constraintTop_toTopOf="parent" />
64+
4965
<com.google.android.material.button.MaterialButtonToggleGroup
5066
android:id="@+id/time_format_toggle"
5167
android:layout_width="wrap_content"
5268
android:layout_height="wrap_content"
53-
android:layout_margin="16dp"
69+
android:layout_marginTop="8dp"
5470
android:orientation="horizontal"
5571
app:layout_constraintEnd_toEndOf="parent"
5672
app:layout_constraintStart_toStartOf="parent"
57-
app:layout_constraintTop_toTopOf="parent"
73+
app:layout_constraintTop_toBottomOf="@+id/time_format_label"
5874
app:selectionRequired="true"
5975
app:singleSelection="true">
76+
77+
<Button
78+
android:id="@+id/time_format_system"
79+
style="?attr/materialButtonOutlinedStyle"
80+
android:layout_width="wrap_content"
81+
android:layout_height="wrap_content"
82+
android:text="@string/time_format_system" />
83+
6084
<Button
6185
android:id="@+id/time_format_12h"
6286
style="?attr/materialButtonOutlinedStyle"
@@ -71,12 +95,71 @@
7195
android:layout_height="wrap_content"
7296
android:text="@string/time_format_24" />
7397

98+
</com.google.android.material.button.MaterialButtonToggleGroup>
99+
100+
<TextView
101+
android:id="@+id/time_input_mode_label"
102+
android:layout_width="0dp"
103+
android:layout_height="wrap_content"
104+
android:layout_marginStart="16dp"
105+
android:layout_marginLeft="16dp"
106+
android:layout_marginTop="16dp"
107+
android:layout_marginEnd="16dp"
108+
android:layout_marginRight="16dp"
109+
android:text="@string/time_input_mode_label"
110+
android:textAlignment="center"
111+
android:textAppearance="?attr/textAppearanceBodyLarge"
112+
app:layout_constraintEnd_toEndOf="parent"
113+
app:layout_constraintStart_toStartOf="parent"
114+
app:layout_constraintTop_toBottomOf="@+id/time_format_toggle" />
115+
116+
<TextView
117+
android:id="@+id/time_input_mode_note"
118+
android:layout_width="0dp"
119+
android:layout_height="wrap_content"
120+
android:layout_marginStart="16dp"
121+
android:layout_marginLeft="16dp"
122+
android:layout_marginEnd="16dp"
123+
android:layout_marginRight="16dp"
124+
android:text="@string/time_input_mode_note"
125+
android:textAlignment="center"
126+
android:textAppearance="?attr/textAppearanceBodySmall"
127+
app:layout_constraintEnd_toEndOf="parent"
128+
app:layout_constraintStart_toStartOf="parent"
129+
app:layout_constraintTop_toBottomOf="@+id/time_input_mode_label" />
130+
131+
<com.google.android.material.button.MaterialButtonToggleGroup
132+
android:id="@+id/time_input_mode_toggle"
133+
android:layout_width="wrap_content"
134+
android:layout_height="wrap_content"
135+
android:layout_marginTop="8dp"
136+
android:orientation="horizontal"
137+
app:layout_constraintEnd_toEndOf="parent"
138+
app:layout_constraintStart_toStartOf="parent"
139+
app:layout_constraintTop_toBottomOf="@+id/time_input_mode_note"
140+
app:selectionRequired="true"
141+
app:singleSelection="true">
142+
74143
<Button
75-
android:id="@+id/time_format_system"
144+
android:id="@+id/time_input_mode_default"
76145
style="?attr/materialButtonOutlinedStyle"
77146
android:layout_width="wrap_content"
78147
android:layout_height="wrap_content"
79-
android:text="@string/time_format_system" />
148+
android:text="@string/time_input_mode_default" />
149+
150+
<Button
151+
android:id="@+id/time_input_mode_clock"
152+
style="?attr/materialButtonOutlinedStyle"
153+
android:layout_width="wrap_content"
154+
android:layout_height="wrap_content"
155+
android:text="@string/time_input_mode_clock" />
156+
157+
<Button
158+
android:id="@+id/time_input_mode_keyboard"
159+
style="?attr/materialButtonOutlinedStyle"
160+
android:layout_width="wrap_content"
161+
android:layout_height="wrap_content"
162+
android:text="@string/time_input_mode_keyboard" />
80163

81164
</com.google.android.material.button.MaterialButtonToggleGroup>
82165

@@ -88,7 +171,7 @@
88171
android:text="@string/use_framework_dialog"
89172
app:layout_constraintEnd_toEndOf="parent"
90173
app:layout_constraintStart_toStartOf="parent"
91-
app:layout_constraintTop_toBottomOf="@+id/time_format_toggle" />
174+
app:layout_constraintTop_toBottomOf="@+id/time_input_mode_toggle" />
92175

93176
</androidx.constraintlayout.widget.ConstraintLayout>
94177

catalog/java/io/material/catalog/timepicker/res/values/strings.xml

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,28 @@
2020
<string name="cat_time_picker_demo_title">Time Picker</string>
2121
<!-- Description of the catalog demo containing time picker. [CHAR LIMIT=128] -->
2222
<string name="cat_time_picker_description">Time pickers allow users to choose times.</string>
23+
<!-- Label for a time format toggle group [CHAR LIMIT=NONE] -->
24+
<string name="time_format_label">Time format</string>
25+
<!-- Label for an option to choose the 12-hour time format [CHAR LIMIT=16] -->
26+
<string name="time_format_24">24H</string>
27+
<!-- Label for an option to choose the 24-hour time format [CHAR LIMIT=16] -->
28+
<string name="time_format_12">12H</string>
29+
<!-- Label for an option to choose the same time format as the system [CHAR LIMIT=16] -->
30+
<string name="time_format_system">System</string>
31+
<!-- Label for a time input mode toggle group [CHAR LIMIT=NONE] -->
32+
<string name="time_input_mode_label">Time input mode</string>
33+
<!-- Label for an option to choose the default time picker's input mode [CHAR LIMIT=16] -->
34+
<string name="time_input_mode_default">Default</string>
35+
<!-- Label for an option to choose the clock mode as the time picker's input mode [CHAR LIMIT=16] -->
36+
<string name="time_input_mode_clock">Clock</string>
37+
<!-- Label for an option to choose the text input mode as the time picker's input mode [CHAR LIMIT=16] -->
38+
<string name="time_input_mode_keyboard">Keyboard</string>
39+
<!-- Text informing that a time input mode selection is not available for Framework Time Picker [CHAR LIMIT=NONE] -->
40+
<string name="time_input_mode_note">(not available for Framework Time Picker)</string>
2341
<!-- Label for a switch that allows the user to use the framework version of the time picker [CHAR LIMIT=128] -->
2442
<string name="use_framework_dialog">Use Framework Time Picker</string>
2543
<!-- Label for a button to choose the time [CHAR LIMIT=64] -->
2644
<string name="select_time">Select Time</string>
2745
<!-- The default time when entering the fragment [CHAR LIMIT=64] -->
2846
<string name="default_time">12:00 AM</string>
29-
<!-- Label for an option to choose the 12 hour time format [CHAR LIMIT=16] -->
30-
<string name="time_format_24">24H</string>
31-
<!-- Label for an option to choose the 24 hour time format [CHAR LIMIT=16] -->
32-
<string name="time_format_12">12H</string>
33-
<!-- Label for an option to choose the same time format as the system [CHAR LIMIT=16] -->
34-
<string name="time_format_system">System</string>
35-
3647
</resources>

0 commit comments

Comments
 (0)