Skip to content

[Dialog] Shrink vertical background insets while the soft keyboard is shown - #5108

Open
jiri001meitner wants to merge 1 commit into
material-components:masterfrom
jiri001meitner:fix/dialog-ime-background-insets
Open

jiri001meitner wants to merge 1 commit into
material-components:masterfrom
jiri001meitner:fix/dialog-ime-background-insets

Conversation

@jiri001meitner

@jiri001meitner jiri001meitner commented Nov 23, 2026 •

Copy link
Copy Markdown

Closes #3418
Closes #1480
Closes #4273
Closes #4039

Problem

MaterialAlertDialogBuilder insets the window background by 80dp at the top and bottom on screens taller than 480dp (10dp otherwise). The insets are fixed. While the soft keyboard is shown, the space left above it may not hold both the insets and the dialog content, so AlertDialogLayout squeezes the custom panel. A text field in the dialog then collapses to zero height, or a panned dialog does not move up far enough. This happens with a larger display size or on a smaller screen. I ran into it in the WireGuard app: the tunnel name field disappears from the "Import tunnel from QR code" dialog.

Fix

The new package-private VerticalBackgroundInsetsAdjuster manages the vertical background insets of the dialog window:

  • While the keyboard is shown, the vertical insets shrink proportionally to the space the content does not need, down to zero. The insets are a margin, so they are given up before the content is squeezed. With the keyboard shown, the dialog then fits like an AppCompat AlertDialog, which has no such margin.
  • The configured insets are restored when the keyboard is hidden.
  • Without the keyboard the insets never change, so tall dialogs, e.g. long lists, keep scrolling within the configured margins.
  • The content is measured with AT_MOST, because with UNSPECIFIED, AlertDialogLayout underestimates stacked buttons.
  • The insets are recalculated on layout and when the window insets report a change of the keyboard visibility. After a change, the window attributes are reapplied, so a window using adjustPan is resized and panned for the new insets.
  • Keyboard detection must not depend on the dialog window: a dialog that shrinks to fit above the keyboard no longer overlaps it, its own insets and visible display frame then report no keyboard, and the layout would oscillate. On API 30+, the visibility comes from the window insets and the keyboard height from WindowManager.getCurrentWindowMetrics(). Below API 30, WindowInsetsCompat does not report the keyboard for dialog windows (the bottom inset stays 0), so the configured screen height is compared with the visible display frame of the dialog window, limited by the visible display frame of the activity window, which covers the keyboard area.

The public API does not change. MaterialAlertDialogBuilder.create() installs the adjuster instead of setting a fixed InsetDrawable and InsetDialogOnTouchListener; both are now updated together with the insets.

Testing

  • Unit tests: VerticalBackgroundInsetsAdjusterTest for the inset calculation. The full :lib:testReleaseUnitTest passes (2875 tests; run with TZ=UTC, because some existing date tests depend on the time zone also on master), and :lib:lintRelease reports no issues. The new code is formatted with google-java-format.
  • Emulator test MaterialAlertDialogImeTest (API 30+, skipped without a soft keyboard): a text field is not collapsed with adjustResize, a dialog with adjustPan fits above the keyboard and its layout settles, and the configured insets are restored after hiding the keyboard. All three fail with 1.14.0 (API 36) and pass with this change (API 30 and 36). The tests module is not included in settings.gradle, so I ran the test in a separate app against publishToMavenLocal.
  • Regression suite on emulators with API 23, 29, 30 and 36, each at the default and a larger display density, comparing 1.14.0 with this change. Scenarios: the TextInputLayout dialog from [TextInputLayout] Collapses on small device when showing keyboard. #1480, the dialog from the [MaterialAlertDialogBuilder] The background inset used by MaterialAlertDialog prevents it from panning up sufficiently when keyboard opens to prevent overlap #4273 sample (adjustPan), the two-field dialog from [MaterialAlertDialogBuilder] Automatic resizing of MaterialAlertDialogBuilder height when the keyboard is open #4039 in the default mode, with adjustResize and with adjustPan, a dialog with a message and a field, and a 50-item list without the keyboard. For each, the suite checks which of the title, fields and buttons are above the keyboard, and that the dialog layout settles after showing and after hiding the keyboard (layout passes counted for 3 s once it is idle). In all 56 runs, nothing visible with 1.14.0 is hidden with this change, and no layout oscillates (at most one late pass, when the insets are restored).
Scenario 1.14.0 This change
adjustResize (#3418, #1480) text field collapsed at higher densities text field visible, insets shrunk (e.g. 300 → 96 px on API 30 @ 600dpi), restored after hiding the keyboard
Two fields (#4039), default mode and adjustPan buttons under the keyboard, on API 30 at 600dpi both fields collapsed fields and buttons visible (except API 23 @ 560dpi, where the dialog does not fit: buttons stay under the keyboard as before, second field now visible)
Two fields (#4039), adjustResize both fields collapsed at higher densities both fields visible
adjustPan (#4273), API 30/36 dialog covered by the keyboard insets 0 while the keyboard is shown, field and buttons visible on API 30 and at the default density on API 36, same as AppCompat AlertDialog
adjustPan (#4273), API 23/29 insets kept insets 0, dialog pans higher. The platform does not resize panned dialog windows below API 30, so the buttons of the tall sample dialog stay under the keyboard, as with AppCompat AlertDialog
Long list without keyboard — unchanged
  • The WireGuard app built against this branch, on an emulator (API 36, 600dpi): the tunnel name field is visible again. An earlier revision of this change also fixed it on a physical phone (display size at the 4th of 5 steps).

Known limitation: below API 30, hiding the keyboard does not always lay out a dialog window that already fits the screen, so the configured insets are restored on its next layout pass. The insets are transparent, so this does not change how such a dialog looks.

Screenshots

WireGuard, "Import tunnel from QR code", API 36, 600dpi, before / after:

#1480 scenario, API 23 (Nexus 5, default 480dpi), before / after:

#4273 sample, API 36, 420dpi: Material before / Material after / AppCompat AlertDialog:

#4273 sample, API 23, 480dpi: Material before / Material after / AppCompat AlertDialog:

@google-cla

google-cla Bot commented Nov 23, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

… shown

MaterialAlertDialogBuilder insets the window background by 80dp at the top
and bottom on screens taller than 480dp. The insets are fixed, so while the
soft keyboard is shown, the space left above it may not hold both the insets
and the dialog content. AlertDialogLayout then squeezes the custom panel,
and a text field in a dialog collapses to zero height, typically with a
larger display size or a smaller screen.

The insets are now adjusted by VerticalBackgroundInsetsAdjuster. While the
keyboard is shown, the vertical insets are shrunk proportionally to the
space that the content does not need, down to zero. The configured insets
are restored when the keyboard is hidden. Without the keyboard the insets
are never changed, so tall dialogs keep scrolling within the configured
margins.

The insets are recalculated when the window is laid out and when the window
insets report a change of the keyboard visibility. After a change the
window attributes are reapplied, so a window using adjustPan is resized and
panned for the new insets.

The keyboard is detected independently of the dialog window: a dialog that
fits above the keyboard no longer overlaps it, and values derived from its
window would report no keyboard and make the layout oscillate. On API 30+
the visibility comes from the window insets and the keyboard height from
the current window metrics. Below API 30, where WindowInsetsCompat does not
report the keyboard for dialog windows, the configured screen height is
compared with the visible display frame of the dialog window, limited by
the visible display frame of the activity window.

MaterialAlertDialogImeTest covers adjustResize, adjustPan with settling of
the layout, and restoring the insets. Tested on emulators with API 23, 29,
30 and 36 at two display densities each, with dialogs from the linked
issues in the default mode, adjustResize and adjustPan, and a tall list
dialog without the keyboard.

Closes material-components#3418
Closes material-components#1480
Closes material-components#4273
Closes material-components#4039
@jiri001meitner
jiri001meitner force-pushed the fix/dialog-ime-background-insets branch from c87e0b2 to e44d2ba Compare November 23, 2026 18:57
private void applyInsets() {
Rect insets = new Rect(currentInsets);
window.setBackgroundDrawable(MaterialDialogs.insetDrawable(background, insets));
window.getDecorView().setOnTouchListener(new InsetDialogOnTouchListener(dialog, insets));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since install() runs inside MaterialAlertDialogBuilder.create(), if a caller overrides dialog.getWindow().setBackgroundDrawable(...) after create(), updateInsets() will silently clobber their custom background with the original background as soon as the IME opens. Keeping a reference to the last InsetDrawable applied here and bailing out when window.getDecorView().getBackground() no longer matches it would avoid overwriting caller-customized backgrounds.

applyInsets();
View decorView = window.getDecorView();
decorView.post(() -> window.setAttributes(window.getAttributes()));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth guarding this posted runnable with if (dialog.isShowing() && decorView.isAttachedToWindow()) so a dialog dismissed right as the IME animates doesn't run setAttributes() against a detached or dismissed window.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants