1818
1919import com .google .android .material .R ;
2020
21+ import static androidx .annotation .RestrictTo .Scope .LIBRARY_GROUP ;
22+
2123import android .app .Dialog ;
2224import android .content .Context ;
2325import android .content .DialogInterface ;
2931import android .graphics .drawable .InsetDrawable ;
3032import android .graphics .drawable .StateListDrawable ;
3133import android .os .Bundle ;
34+ import androidx .annotation .IntDef ;
3235import androidx .annotation .NonNull ;
3336import androidx .annotation .Nullable ;
37+ import androidx .annotation .RestrictTo ;
3438import androidx .annotation .StringRes ;
3539import androidx .annotation .StyleRes ;
3640import androidx .fragment .app .DialogFragment ;
5054import com .google .android .material .internal .CheckableImageButton ;
5155import com .google .android .material .resources .MaterialAttributes ;
5256import com .google .android .material .shape .MaterialShapeDrawable ;
57+ import java .lang .annotation .Retention ;
58+ import java .lang .annotation .RetentionPolicy ;
5359import java .util .LinkedHashSet ;
5460
5561/** A {@link Dialog} with a header, {@link MaterialCalendar}, and set of actions. */
@@ -60,11 +66,24 @@ public final class MaterialDatePicker<S> extends DialogFragment {
6066 private static final String CALENDAR_CONSTRAINTS_KEY = "CALENDAR_CONSTRAINTS_KEY" ;
6167 private static final String TITLE_TEXT_RES_ID_KEY = "TITLE_TEXT_RES_ID_KEY" ;
6268 private static final String TITLE_TEXT_KEY = "TITLE_TEXT_KEY" ;
69+ private static final String INPUT_MODE_KEY = "INPUT_MODE_KEY" ;
6370
6471 static final Object CONFIRM_BUTTON_TAG = "CONFIRM_BUTTON_TAG" ;
6572 static final Object CANCEL_BUTTON_TAG = "CANCEL_BUTTON_TAG" ;
6673 static final Object TOGGLE_BUTTON_TAG = "TOGGLE_BUTTON_TAG" ;
6774
75+ /** Date picker will start with calendar view. */
76+ public static final int INPUT_MODE_CALENDAR = 0 ;
77+
78+ /** Date picker will start with input text view. */
79+ public static final int INPUT_MODE_TEXT = 1 ;
80+
81+ /** @hide */
82+ @ RestrictTo (LIBRARY_GROUP )
83+ @ IntDef (value = {INPUT_MODE_CALENDAR , INPUT_MODE_TEXT })
84+ @ Retention (RetentionPolicy .SOURCE )
85+ public @interface InputMode {}
86+
6887 /** Returns the UTC milliseconds representing the first moment of today in local timezone. */
6988 public static long todayInUtcMilliseconds () {
7089 return UtcDates .getTodayCalendar ().getTimeInMillis ();
@@ -103,6 +122,7 @@ public String getHeaderText() {
103122 @ StringRes private int titleTextResId ;
104123 private CharSequence titleText ;
105124 private boolean fullscreen ;
125+ @ InputMode private int inputMode ;
106126
107127 private TextView headerSelectionText ;
108128 private CheckableImageButton headerToggleButton ;
@@ -118,6 +138,7 @@ static <S> MaterialDatePicker<S> newInstance(@NonNull Builder<S> options) {
118138 args .putParcelable (CALENDAR_CONSTRAINTS_KEY , options .calendarConstraints );
119139 args .putInt (TITLE_TEXT_RES_ID_KEY , options .titleTextResId );
120140 args .putCharSequence (TITLE_TEXT_KEY , options .titleText );
141+ args .putInt (INPUT_MODE_KEY , options .inputMode );
121142 materialDatePickerDialogFragment .setArguments (args );
122143 return materialDatePickerDialogFragment ;
123144 }
@@ -147,6 +168,7 @@ public final void onCreate(@Nullable Bundle bundle) {
147168 calendarConstraints = activeBundle .getParcelable (CALENDAR_CONSTRAINTS_KEY );
148169 titleTextResId = activeBundle .getInt (TITLE_TEXT_RES_ID_KEY );
149170 titleText = activeBundle .getCharSequence (TITLE_TEXT_KEY );
171+ inputMode = activeBundle .getInt (INPUT_MODE_KEY );
150172 }
151173
152174 private int getThemeResId (Context context ) {
@@ -339,6 +361,8 @@ public void onSelectionChanged(S selection) {
339361 private void initHeaderToggle (Context context ) {
340362 headerToggleButton .setTag (TOGGLE_BUTTON_TAG );
341363 headerToggleButton .setImageDrawable (createHeaderToggleDrawable (context ));
364+ headerToggleButton .setChecked (inputMode != INPUT_MODE_CALENDAR );
365+
342366 // By default, CheckableImageButton adds a delegate that reads checked state.
343367 // This information is not useful; we remove the delegate and use custom content descriptions.
344368 ViewCompat .setAccessibilityDelegate (headerToggleButton , null );
@@ -504,6 +528,7 @@ public static final class Builder<S> {
504528 int titleTextResId = 0 ;
505529 CharSequence titleText = null ;
506530 @ Nullable S selection = null ;
531+ @ InputMode int inputMode = INPUT_MODE_CALENDAR ;
507532
508533 private Builder (DateSelector <S > dateSelector ) {
509534 this .dateSelector = dateSelector ;
@@ -575,6 +600,13 @@ public Builder<S> setTitleText(@Nullable CharSequence charSequence) {
575600 return this ;
576601 }
577602
603+ /** Sets the input mode to start with. */
604+ @ NonNull
605+ public Builder <S > setInputMode (@ InputMode int inputMode ) {
606+ this .inputMode = inputMode ;
607+ return this ;
608+ }
609+
578610 /** Creates a {@link MaterialDatePicker} with the provided options. */
579611 @ NonNull
580612 public MaterialDatePicker <S > build () {
0 commit comments