Set Height of Alert Dialog

Issue

I have 2-3 radio buttons in my alert dialog but the size of the dialog is not wrapping them up even though the screen could easily fit them instead putting them in a small layout with scroll view.

Image

I referred to this question How to control the width and height of the default Alert Dialog in Android? and tried may of the answers to increase the height of the alert dialog but unsuccessful. The result was this when I tried setting layout size.

Image

Code:

AlertDialog.Builder(requireContext()).setTitle("Choose Address")
        .setSingleChoiceItems(addresses, selectedItemIndex) { _, which ->
            selectedItemIndex = which
        }.setPositiveButton("Confirm Address") { dialog, _ ->
            confirmBooking(selectedItemIndex + 1, user)
            dialog.dismiss()
        }.setNeutralButton("Cancel") { dialog, _ ->
            dialog.dismiss()
        }.show()
        .window?.setLayout(
            (resources.displayMetrics.widthPixels * 0.9).toInt(),
            (resources.displayMetrics.heightPixels * 0.7).toInt()
        )

I wanted to use MaterialAlertDialog but that was also giving the same results. I do not want to use a custom dialog. How can I solve this height problem?

Please comment if any other information is required. I will be grateful for any help. Thanks in advance.

Solution

For what it’s worth, using your code (just the dialog builder, not the window-tweaking bit at the end) I get:

import android.app.AlertDialog – expands properly at Default font size in the system settings, stays small and scrolls at Largest. (Almost like it’s trying to maintain a consistent size, based on the space needed for Default)

import androidx.appcompat.app.AlertDialog – expands properly at Default and Largest

That’s using appcompat:1.4.0 with an old test project on an API 30 emulator, should be the same on the latest version I expect!

Answered By – cactustictacs

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *