Skip to content

Commit e1ee959

Browse files
drchendsn5ft
authored andcommitted
[DatePicker] Fix edge-to-edge mode being applied multiple times
When the date picker goes back from background to foreground, the onStart() method will be called and make enableEdgeToEdge() be called again. This will make it incorrectly use the header height and top padding already adjusted with system inset as the original height and padding, and thus add the inset multiple times. Creates a flag to only enable edge-to-edge once to fix the issue. Resolves #2628 PiperOrigin-RevId: 440110562 (cherry picked from commit c6a654c)
1 parent bed5c59 commit e1ee959

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public String getHeaderText() {
141141
@Nullable private MaterialShapeDrawable background;
142142
private Button confirmButton;
143143

144+
private boolean edgeToEdgeEnabled;
145+
144146
@NonNull
145147
static <S> MaterialDatePicker<S> newInstance(@NonNull Builder<S> options) {
146148
MaterialDatePicker<S> materialDatePickerDialogFragment = new MaterialDatePicker<>();
@@ -307,7 +309,7 @@ public void onStart() {
307309
if (fullscreen) {
308310
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
309311
window.setBackgroundDrawable(background);
310-
enableEdgeToEdge(window);
312+
enableEdgeToEdgeIfNeeded(window);
311313
} else {
312314
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
313315
int inset =
@@ -356,7 +358,11 @@ public final S getSelection() {
356358
return getDateSelector().getSelection();
357359
}
358360

359-
private void enableEdgeToEdge(Window window) {
361+
private void enableEdgeToEdgeIfNeeded(Window window) {
362+
if (edgeToEdgeEnabled) {
363+
// Avoid enabling edge-to-edge multiple times.
364+
return;
365+
}
360366
final View headerLayout = requireView().findViewById(R.id.fullscreen_header);
361367
EdgeToEdgeUtils.applyEdgeToEdge(
362368
window, true, ViewUtils.getBackgroundColor(headerLayout), null);
@@ -380,6 +386,7 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
380386
return insets;
381387
}
382388
});
389+
edgeToEdgeEnabled = true;
383390
}
384391

385392
private void updateHeader() {

0 commit comments

Comments
 (0)