Skip to content

Commit

Permalink
[DatePicker] Fix edge-to-edge mode being applied multiple times
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
drchen authored and dsn5ft committed Apr 19, 2022
1 parent bed5c59 commit e1ee959
Showing 1 changed file with 9 additions and 2 deletions.
Expand Up @@ -141,6 +141,8 @@ public String getHeaderText() {
@Nullable private MaterialShapeDrawable background;
private Button confirmButton;

private boolean edgeToEdgeEnabled;

@NonNull
static <S> MaterialDatePicker<S> newInstance(@NonNull Builder<S> options) {
MaterialDatePicker<S> materialDatePickerDialogFragment = new MaterialDatePicker<>();
Expand Down Expand Up @@ -307,7 +309,7 @@ public void onStart() {
if (fullscreen) {
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
window.setBackgroundDrawable(background);
enableEdgeToEdge(window);
enableEdgeToEdgeIfNeeded(window);
} else {
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int inset =
Expand Down Expand Up @@ -356,7 +358,11 @@ public final S getSelection() {
return getDateSelector().getSelection();
}

private void enableEdgeToEdge(Window window) {
private void enableEdgeToEdgeIfNeeded(Window window) {
if (edgeToEdgeEnabled) {
// Avoid enabling edge-to-edge multiple times.
return;
}
final View headerLayout = requireView().findViewById(R.id.fullscreen_header);
EdgeToEdgeUtils.applyEdgeToEdge(
window, true, ViewUtils.getBackgroundColor(headerLayout), null);
Expand All @@ -380,6 +386,7 @@ public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets)
return insets;
}
});
edgeToEdgeEnabled = true;
}

private void updateHeader() {
Expand Down

0 comments on commit e1ee959

Please sign in to comment.