38
38
import android .text .TextUtils ;
39
39
import android .view .LayoutInflater ;
40
40
import android .view .View ;
41
- import android .view .View .OnClickListener ;
42
41
import android .view .ViewGroup ;
43
42
import android .view .Window ;
44
43
import android .widget .Button ;
@@ -188,6 +187,7 @@ public final void onSaveInstanceState(@NonNull Bundle bundle) {
188
187
bundle .putParcelable (DAY_VIEW_DECORATOR_KEY , dayViewDecorator );
189
188
bundle .putInt (TITLE_TEXT_RES_ID_KEY , titleTextResId );
190
189
bundle .putCharSequence (TITLE_TEXT_KEY , titleText );
190
+ bundle .putInt (INPUT_MODE_KEY , inputMode );
191
191
bundle .putInt (POSITIVE_BUTTON_TEXT_RES_ID_KEY , positiveButtonTextResId );
192
192
bundle .putCharSequence (POSITIVE_BUTTON_TEXT_KEY , positiveButtonText );
193
193
bundle .putInt (NEGATIVE_BUTTON_TEXT_RES_ID_KEY , negativeButtonTextResId );
@@ -384,6 +384,12 @@ public final S getSelection() {
384
384
return getDateSelector ().getSelection ();
385
385
}
386
386
387
+ /** Returns the current {@link InputMode}. */
388
+ @ InputMode
389
+ public int getInputMode () {
390
+ return inputMode ;
391
+ }
392
+
387
393
private void enableEdgeToEdgeIfNeeded (Window window ) {
388
394
if (edgeToEdgeEnabled ) {
389
395
// Avoid enabling edge-to-edge multiple times.
@@ -415,10 +421,10 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
415
421
edgeToEdgeEnabled = true ;
416
422
}
417
423
418
- private void updateTitle (boolean textInputMode ) {
424
+ private void updateTitle () {
419
425
// Set up title text forcing single line for landscape text input mode due to space constraints.
420
426
headerTitleTextView .setText (
421
- textInputMode && isLandscape () ? singleLineTitleText : fullTitleText );
427
+ inputMode == INPUT_MODE_TEXT && isLandscape () ? singleLineTitleText : fullTitleText );
422
428
}
423
429
424
430
@ VisibleForTesting
@@ -436,13 +442,13 @@ private void startPickerFragment() {
436
442
calendar =
437
443
MaterialCalendar .newInstance (
438
444
getDateSelector (), themeResId , calendarConstraints , dayViewDecorator );
439
- boolean textInputMode = headerToggleButton . isChecked ();
445
+
440
446
pickerFragment =
441
- textInputMode
447
+ inputMode == INPUT_MODE_TEXT
442
448
? MaterialTextInputPicker .newInstance (
443
449
getDateSelector (), themeResId , calendarConstraints )
444
450
: calendar ;
445
- updateTitle (textInputMode );
451
+ updateTitle ();
446
452
updateHeader (getHeaderText ());
447
453
448
454
FragmentTransaction fragmentTransaction = getChildFragmentManager ().beginTransaction ();
@@ -474,22 +480,20 @@ private void initHeaderToggle(Context context) {
474
480
ViewCompat .setAccessibilityDelegate (headerToggleButton , null );
475
481
updateToggleContentDescription (headerToggleButton );
476
482
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 ();
487
491
});
488
492
}
489
493
490
494
private void updateToggleContentDescription (@ NonNull CheckableImageButton toggle ) {
491
495
String contentDescription =
492
- headerToggleButton . isChecked ()
496
+ inputMode == INPUT_MODE_TEXT
493
497
? toggle .getContext ().getString (R .string .mtrl_picker_toggle_to_calendar_input_mode )
494
498
: toggle .getContext ().getString (R .string .mtrl_picker_toggle_to_text_input_mode );
495
499
headerToggleButton .setContentDescription (contentDescription );
0 commit comments