Skip to content

Commit 4d80434

Browse files
committed
[MaterialDatePicker] Allow client app to access user selected inputMode
Resolves #3414 PiperOrigin-RevId: 539713099
1 parent 2336c23 commit 4d80434

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

lib/java/com/google/android/material/datepicker/MaterialDatePicker.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import android.text.TextUtils;
3939
import android.view.LayoutInflater;
4040
import android.view.View;
41-
import android.view.View.OnClickListener;
4241
import android.view.ViewGroup;
4342
import android.view.Window;
4443
import android.widget.Button;
@@ -188,6 +187,7 @@ public final void onSaveInstanceState(@NonNull Bundle bundle) {
188187
bundle.putParcelable(DAY_VIEW_DECORATOR_KEY, dayViewDecorator);
189188
bundle.putInt(TITLE_TEXT_RES_ID_KEY, titleTextResId);
190189
bundle.putCharSequence(TITLE_TEXT_KEY, titleText);
190+
bundle.putInt(INPUT_MODE_KEY, inputMode);
191191
bundle.putInt(POSITIVE_BUTTON_TEXT_RES_ID_KEY, positiveButtonTextResId);
192192
bundle.putCharSequence(POSITIVE_BUTTON_TEXT_KEY, positiveButtonText);
193193
bundle.putInt(NEGATIVE_BUTTON_TEXT_RES_ID_KEY, negativeButtonTextResId);
@@ -384,6 +384,12 @@ public final S getSelection() {
384384
return getDateSelector().getSelection();
385385
}
386386

387+
/** Returns the current {@link InputMode}. */
388+
@InputMode
389+
public int getInputMode() {
390+
return inputMode;
391+
}
392+
387393
private void enableEdgeToEdgeIfNeeded(Window window) {
388394
if (edgeToEdgeEnabled) {
389395
// Avoid enabling edge-to-edge multiple times.
@@ -415,10 +421,10 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
415421
edgeToEdgeEnabled = true;
416422
}
417423

418-
private void updateTitle(boolean textInputMode) {
424+
private void updateTitle() {
419425
// Set up title text forcing single line for landscape text input mode due to space constraints.
420426
headerTitleTextView.setText(
421-
textInputMode && isLandscape() ? singleLineTitleText : fullTitleText);
427+
inputMode == INPUT_MODE_TEXT && isLandscape() ? singleLineTitleText : fullTitleText);
422428
}
423429

424430
@VisibleForTesting
@@ -436,13 +442,13 @@ private void startPickerFragment() {
436442
calendar =
437443
MaterialCalendar.newInstance(
438444
getDateSelector(), themeResId, calendarConstraints, dayViewDecorator);
439-
boolean textInputMode = headerToggleButton.isChecked();
445+
440446
pickerFragment =
441-
textInputMode
447+
inputMode == INPUT_MODE_TEXT
442448
? MaterialTextInputPicker.newInstance(
443449
getDateSelector(), themeResId, calendarConstraints)
444450
: calendar;
445-
updateTitle(textInputMode);
451+
updateTitle();
446452
updateHeader(getHeaderText());
447453

448454
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
@@ -474,22 +480,20 @@ private void initHeaderToggle(Context context) {
474480
ViewCompat.setAccessibilityDelegate(headerToggleButton, null);
475481
updateToggleContentDescription(headerToggleButton);
476482
headerToggleButton.setOnClickListener(
477-
new OnClickListener() {
478-
@Override
479-
public void onClick(View v) {
480-
// Update confirm button in case in progress selection has been reset
481-
confirmButton.setEnabled(getDateSelector().isSelectionComplete());
482-
483-
headerToggleButton.toggle();
484-
updateToggleContentDescription(headerToggleButton);
485-
startPickerFragment();
486-
}
483+
v -> {
484+
// Update confirm button in case in progress selection has been reset
485+
confirmButton.setEnabled(getDateSelector().isSelectionComplete());
486+
487+
headerToggleButton.toggle();
488+
inputMode = (inputMode == INPUT_MODE_TEXT) ? INPUT_MODE_CALENDAR : INPUT_MODE_TEXT;
489+
updateToggleContentDescription(headerToggleButton);
490+
startPickerFragment();
487491
});
488492
}
489493

490494
private void updateToggleContentDescription(@NonNull CheckableImageButton toggle) {
491495
String contentDescription =
492-
headerToggleButton.isChecked()
496+
inputMode == INPUT_MODE_TEXT
493497
? toggle.getContext().getString(R.string.mtrl_picker_toggle_to_calendar_input_mode)
494498
: toggle.getContext().getString(R.string.mtrl_picker_toggle_to_text_input_mode);
495499
headerToggleButton.setContentDescription(contentDescription);

lib/javatests/com/google/android/material/datepicker/MaterialDatePickerTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.android.material.datepicker;
1818

19+
import static com.google.android.material.datepicker.MaterialDatePicker.INPUT_MODE_CALENDAR;
1920
import static com.google.common.truth.Truth.assertThat;
2021
import static java.util.Calendar.APRIL;
2122
import static java.util.Calendar.FEBRUARY;
@@ -95,7 +96,6 @@ private static void testThisMonthInUtcMillisecondsForLocalTime(
9596

9697
@Test
9798
public void testSelectionAsOpenAt() {
98-
9999
MaterialDatePicker.Builder<Long> datePickerBuilder = MaterialDatePicker.Builder.datePicker();
100100
CalendarConstraints calendarConstraints =
101101
new CalendarConstraints.Builder().setStart(FEB_2016).setEnd(APRIL_2016).build();
@@ -178,4 +178,11 @@ public void testFirstDayOfWeekAsDefault() {
178178
datePickerBuilder.build();
179179
assertEquals(0, calendarConstraints.getFirstDayOfWeek());
180180
}
181+
182+
@Test
183+
public void testInputModeCalendarAsDefault() {
184+
MaterialDatePicker<Long> materialDatePicker = MaterialDatePicker.Builder.datePicker().build();
185+
186+
assertEquals(INPUT_MODE_CALENDAR, materialDatePicker.getInputMode());
187+
}
181188
}

0 commit comments

Comments
 (0)