Skip to content

Commit 6b35ef1

Browse files
committed
[MaterialDatePicker] Add bottom button bar to fullscreen picker (incl. 'Clear')
The design suggestion is following the spec here: https://m3.material.io/components/date-pickers/specs#f25f68b7-1bb5-4248-829e-fe3cd3499dab And adding only the clear button to the bottom button bar
1 parent 4186ef0 commit 6b35ef1

5 files changed

Lines changed: 140 additions & 20 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public String getHeaderText() {
175175
private CheckableImageButton headerToggleButton;
176176
@Nullable private MaterialShapeDrawable background;
177177
private Button confirmButton;
178+
private Button headerConfirmButton;
178179
private Button clearButton;
179180
private boolean isClearable;
180181
private boolean dismissOnClear;
@@ -376,6 +377,47 @@ public final View onCreateView(
376377
}
377378
cancelButton.setOnClickListener(this::onNegativeButtonClick);
378379

380+
// In fullscreen mode we have a confirm and cancel button in the header as well
381+
if (fullscreen) {
382+
headerConfirmButton = root.findViewById(R.id.header_confirm_button);
383+
headerConfirmButton.setEnabled(getDateSelector().isSelectionComplete());
384+
headerConfirmButton.setTag(CONFIRM_BUTTON_TAG);
385+
if (positiveButtonText != null) {
386+
headerConfirmButton.setText(positiveButtonText);
387+
} else if (positiveButtonTextResId != 0) {
388+
headerConfirmButton.setText(positiveButtonTextResId);
389+
}
390+
headerConfirmButton.setOnClickListener(
391+
new View.OnClickListener() {
392+
@Override
393+
public void onClick(View v) {
394+
for (MaterialPickerOnPositiveButtonClickListener<? super S> listener :
395+
onPositiveButtonClickListeners) {
396+
listener.onPositiveButtonClick(getSelection());
397+
}
398+
dismiss();
399+
}
400+
});
401+
402+
Button headerCancelButton = root.findViewById(R.id.header_cancel_button);
403+
headerCancelButton.setTag(CANCEL_BUTTON_TAG);
404+
if (negativeButtonText != null) {
405+
headerCancelButton.setText(negativeButtonText);
406+
} else if (negativeButtonTextResId != 0) {
407+
headerCancelButton.setText(negativeButtonTextResId);
408+
}
409+
headerCancelButton.setOnClickListener(
410+
new View.OnClickListener() {
411+
@Override
412+
public void onClick(View v) {
413+
for (View.OnClickListener listener : onNegativeButtonClickListeners) {
414+
listener.onClick(v);
415+
}
416+
dismiss();
417+
}
418+
});
419+
}
420+
379421
clearButton = root.findViewById(R.id.clear_button);
380422
clearButton.setTag(CLEAR_BUTTON_TAG);
381423
if (isClearable) {
@@ -577,6 +619,7 @@ public void onSelectionChanged(S selection) {
577619
@Override
578620
public void onIncompleteSelectionChanged() {
579621
confirmButton.setEnabled(false);
622+
if (fullscreen) headerConfirmButton.setEnabled(false);
580623
clearButton.setEnabled(false);
581624
}
582625
});
@@ -615,6 +658,7 @@ private void initHeaderToggle(Context context) {
615658

616659
private void updateActionButtonEnabledState() {
617660
confirmButton.setEnabled(getDateSelector().isSelectionComplete());
661+
if (fullscreen) headerConfirmButton.setEnabled(getDateSelector().isSelectionComplete());
618662
clearButton.setEnabled(isClearable && getDateSelector().isSelectionComplete());
619663
}
620664

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2019 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:id="@+id/date_picker_actions"
20+
android:layout_width="match_parent"
21+
android:layout_height="@dimen/mtrl_calendar_action_height_fullscreen"
22+
android:paddingStart="@dimen/mtrl_calendar_action_padding_fullscreen"
23+
android:paddingLeft="@dimen/mtrl_calendar_action_padding_fullscreen"
24+
android:paddingEnd="@dimen/mtrl_calendar_action_padding_fullscreen"
25+
android:paddingRight="@dimen/mtrl_calendar_action_padding_fullscreen"
26+
android:gravity="end"
27+
android:orientation="horizontal">
28+
29+
<Button
30+
android:id="@+id/clear_button"
31+
style="?attr/buttonBarNeutralButtonStyle"
32+
android:layout_width="wrap_content"
33+
android:layout_height="wrap_content"
34+
android:layout_gravity="center_vertical"
35+
android:text="@string/mtrl_picker_clear"/>
36+
37+
<Space
38+
android:layout_width="0dp"
39+
android:layout_height="0dp"
40+
android:layout_weight="1"
41+
android:visibility="invisible" />
42+
43+
<Button
44+
android:id="@+id/cancel_button"
45+
style="?attr/buttonBarNegativeButtonStyle"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:layout_gravity="center_vertical"
49+
android:text="@string/mtrl_picker_cancel"/>
50+
51+
<Button
52+
android:id="@+id/confirm_button"
53+
style="?attr/buttonBarPositiveButtonStyle"
54+
android:layout_width="wrap_content"
55+
android:layout_height="wrap_content"
56+
android:layout_gravity="center_vertical"
57+
android:minWidth="@dimen/mtrl_calendar_action_confirm_button_min_width"
58+
android:text="@string/mtrl_picker_confirm"/>
59+
60+
</LinearLayout>

lib/java/com/google/android/material/datepicker/res/layout/mtrl_picker_fullscreen.xml

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,35 @@
2424

2525
<include layout="@layout/mtrl_picker_header_fullscreen"/>
2626

27-
<androidx.fragment.app.FragmentContainerView
27+
<LinearLayout
28+
android:layout_width="match_parent"
29+
android:layout_height="0dp"
30+
android:layout_weight="1"
31+
android:paddingStart="@dimen/mtrl_calendar_content_padding"
32+
android:paddingEnd="@dimen/mtrl_calendar_content_padding"
33+
android:paddingLeft="@dimen/mtrl_calendar_content_padding"
34+
android:paddingRight="@dimen/mtrl_calendar_content_padding"
35+
android:orientation="vertical"
36+
android:gravity="center_horizontal">
37+
38+
<androidx.fragment.app.FragmentContainerView
2839
android:id="@+id/mtrl_calendar_frame"
2940
android:layout_width="match_parent"
30-
android:layout_height="wrap_content"
31-
android:paddingStart="@dimen/mtrl_calendar_content_padding"
32-
android:paddingEnd="@dimen/mtrl_calendar_content_padding"
33-
android:paddingLeft="@dimen/mtrl_calendar_content_padding"
34-
android:paddingRight="@dimen/mtrl_calendar_content_padding"/>
41+
android:layout_height="wrap_content" />
42+
</LinearLayout>
43+
44+
<View
45+
style="?attr/materialCalendarHeaderDivider"
46+
android:layout_width="match_parent"
47+
android:layout_height="@dimen/mtrl_calendar_header_divider_thickness"
48+
android:paddingTop="@dimen/mtrl_calendar_navigation_top_padding"
49+
android:paddingBottom="@dimen/mtrl_calendar_navigation_bottom_padding"
50+
android:layout_gravity="center_vertical"/>
51+
52+
<include
53+
layout="@layout/mtrl_picker_actions_fullscreen"
54+
android:layout_width="match_parent"
55+
android:layout_height="@dimen/mtrl_calendar_action_height_fullscreen"
56+
android:layout_marginBottom="@dimen/mtrl_calendar_action_padding_fullscreen"/>
3557

3658
</LinearLayout>

lib/java/com/google/android/material/datepicker/res/layout/mtrl_picker_header_fullscreen.xml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
android:paddingStart="@dimen/mtrl_calendar_header_content_padding_fullscreen"
3131
android:paddingLeft="@dimen/mtrl_calendar_header_content_padding_fullscreen"
3232
android:paddingTop="@dimen/mtrl_calendar_header_content_padding_fullscreen"
33-
android:paddingEnd="@dimen/mtrl_calendar_header_content_padding_fullscreen"
34-
android:paddingRight="@dimen/mtrl_calendar_header_content_padding_fullscreen"
33+
android:paddingEnd="@dimen/mtrl_calendar_content_padding_fullscreen_right"
34+
android:paddingRight="@dimen/mtrl_calendar_content_padding_fullscreen_right"
3535
tools:ignore="Overdraw">
3636

3737
<com.google.android.material.button.MaterialButton
38-
android:id="@+id/cancel_button"
38+
android:id="@+id/header_cancel_button"
3939
style="?attr/materialCalendarHeaderCancelButton"
4040
android:layout_width="@dimen/mtrl_min_touch_target_size"
4141
android:layout_height="@dimen/mtrl_min_touch_target_size"
@@ -77,15 +77,6 @@
7777
app:firstBaselineToTopHeight="@dimen/mtrl_calendar_selection_baseline_to_top_fullscreen"
7878
app:lineHeight="@dimen/mtrl_calendar_header_selection_line_height"/>
7979

80-
<Button
81-
android:id="@+id/clear_button"
82-
style="?attr/materialCalendarHeaderConfirmButton"
83-
android:layout_width="wrap_content"
84-
android:layout_height="@dimen/mtrl_min_touch_target_size"
85-
android:layout_gravity="end|top"
86-
android:contentDescription="@string/mtrl_picker_clear"
87-
android:text="@string/mtrl_picker_clear"/>
88-
8980
</FrameLayout>
9081

9182
<LinearLayout
@@ -94,10 +85,10 @@
9485
android:orientation="@integer/mtrl_calendar_header_orientation">
9586

9687
<com.google.android.material.button.MaterialButton
97-
android:id="@+id/confirm_button"
88+
android:id="@+id/header_confirm_button"
9889
style="?attr/materialCalendarHeaderConfirmButton"
9990
android:layout_width="wrap_content"
100-
android:layout_height="@dimen/mtrl_min_touch_target_size"
91+
android:layout_height="@dimen/mtrl_calendar_action_height_fullscreen"
10192
android:layout_gravity="end|top"
10293
android:contentDescription="@string/mtrl_picker_save"
10394
android:text="@string/mtrl_picker_save"/>

lib/java/com/google/android/material/datepicker/res/values/dimens.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
<dimen name="mtrl_calendar_header_text_padding">12dp</dimen>
2626
<dimen name="mtrl_calendar_day_today_stroke">1dp</dimen>
2727
<dimen name="mtrl_calendar_content_padding">12dp</dimen>
28+
<dimen name="mtrl_calendar_content_padding_fullscreen_right">12dp</dimen>
2829
<dimen name="mtrl_calendar_navigation_height">48dp</dimen>
2930
<dimen name="mtrl_calendar_navigation_top_padding">4dp</dimen>
3031
<dimen name="mtrl_calendar_navigation_bottom_padding">4dp</dimen>
3132
<dimen name="mtrl_calendar_month_vertical_padding">0dp</dimen>
3233
<dimen name="mtrl_calendar_action_height">52dp</dimen>
34+
<dimen name="mtrl_calendar_action_height_fullscreen">40dp</dimen>
3335
<dimen name="mtrl_calendar_action_padding">8dp</dimen>
36+
<dimen name="mtrl_calendar_action_padding_fullscreen">12dp</dimen>
3437
<dimen name="mtrl_calendar_action_confirm_button_min_width">64dp</dimen>
3538
<dimen name="mtrl_calendar_header_content_padding_fullscreen">4dp</dimen>
3639
<dimen name="mtrl_calendar_header_selection_line_height">32dp</dimen>

0 commit comments

Comments
 (0)