3838import android .text .TextUtils ;
3939import android .view .LayoutInflater ;
4040import android .view .View ;
41- import android .view .View .OnClickListener ;
4241import android .view .ViewGroup ;
4342import android .view .Window ;
4443import 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 );
0 commit comments