Repository navigation
[Dialog] Shrink vertical background insets while the soft keyboard is shown - #5108
jiri001meitner wants to merge 1 commit into
Conversation
|
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
c87e0b2 to
e44d2ba
Compare
| private void applyInsets() { | ||
| Rect insets = new Rect(currentInsets); | ||
| window.setBackgroundDrawable(MaterialDialogs.insetDrawable(background, insets)); | ||
| window.getDecorView().setOnTouchListener(new InsetDialogOnTouchListener(dialog, insets)); |
There was a problem hiding this comment.
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())); | ||
| } |
There was a problem hiding this comment.
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.
Closes #3418
Closes #1480
Closes #4273
Closes #4039
Problem
MaterialAlertDialogBuilderinsets 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, soAlertDialogLayoutsqueezes 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
VerticalBackgroundInsetsAdjustermanages the vertical background insets of the dialog window:AlertDialog, which has no such margin.AT_MOST, because withUNSPECIFIED,AlertDialogLayoutunderestimates stacked buttons.adjustPanis resized and panned for the new insets.WindowManager.getCurrentWindowMetrics(). Below API 30,WindowInsetsCompatdoes 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 fixedInsetDrawableandInsetDialogOnTouchListener; both are now updated together with the insets.Testing
VerticalBackgroundInsetsAdjusterTestfor the inset calculation. The full:lib:testReleaseUnitTestpasses (2875 tests; run withTZ=UTC, because some existing date tests depend on the time zone also onmaster), and:lib:lintReleasereports no issues. The new code is formatted with google-java-format.MaterialAlertDialogImeTest(API 30+, skipped without a soft keyboard): a text field is not collapsed withadjustResize, a dialog withadjustPanfits 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). Thetestsmodule is not included insettings.gradle, so I ran the test in a separate app againstpublishToMavenLocal.TextInputLayoutdialog 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, withadjustResizeand withadjustPan, 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).adjustResize(#3418, #1480)adjustPanadjustResizeadjustPan(#4273), API 30/36AlertDialogadjustPan(#4273), API 23/29AlertDialogKnown 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: