0

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.

Gaurav
  • 197
  • 2
  • 15
  • [This answer](https://stackoverflow.com/a/59922753/6287910) to a similar question may help you. – Cheticamp Apr 12 '22 at 15:52
  • @Cheticamp That code seemed really helpful but how will I get access to the size of the child scroll view/the scroll view that is automatically added to the AlertDialog? or do I need to create my custom Alert Dialog Layout. I would be really great if I could access the height of the child scroll view and then force it using the code you provided. – Gaurav Apr 12 '22 at 16:35
  • 1
    I think that [this](https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/res/res/layout/alert_dialog.xml;l=3;drc=bce4116daf2f056ba5233561e6139fe9033d1126;bpv=0;bpt=0) is the default layout for alert dialogs. IMO, using a custom layout, even if it is based on this layout, is a better practice than relying on the internals of the layout. – Cheticamp Apr 12 '22 at 19:20

2 Answers2

1

If you want dialog size to grow with content height. Then it's basically a wrap content. You can apply ViewGroup.LayoutParams.WRAP_CONTENT to your height.

Following is the slight modification to your code snippet:

    val addresses = arrayOf("1", "2", "3","4","5","6")
    var selectedItemIndex = 0
    private fun showDialog() {

        AlertDialog.Builder(this).setTitle("Choose Address")
            .setSingleChoiceItems(addresses, selectedItemIndex) { _, which ->
                selectedItemIndex = which
            }.setPositiveButton("Confirm Address") { dialog, _ ->
                dialog.dismiss()
            }.setNeutralButton("Cancel") { dialog, _ ->
                dialog.dismiss()
            }.show()
            .window?.setLayout(
                (resources.displayMetrics.widthPixels * 0.9).toInt(),
                ViewGroup.LayoutParams.WRAP_CONTENT
            )

    }

Output:

dialog size

With the strings you provided:

dialog size 2

Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41
  • It is not working with array of text , your example works fine but when I use medium-long length text in array it does not wrap. – Gaurav Apr 12 '22 at 15:49
  • Even one line text is working but as soon as the text is of 2-3 lines the view does not wrap it. – Gaurav Apr 12 '22 at 15:51
  • @Gaurav I've tried with long text as well for example: `1mckdmckdcmdkcmdkcmdkcmdkcmdkcmdkcmkdcmkdcmkdcmdkcmdkcd`. Wrap content works well. Can you please share your text samples & update that in the question. So that I can try at my end? – Mayur Gajra Apr 12 '22 at 15:56
  • Here are the exact text samples I am using -"This address is to test my cloud functions and other stuff. , 153002" and "This is my second address ,this is second address , 152002" , I checked maybe because that test string you used didn't have space it worked. – Gaurav Apr 12 '22 at 16:03
  • When I added couple of spaces to that long text it didn't work as well. – Gaurav Apr 12 '22 at 16:04
  • @Gaurav Check out the SS with your strings provided. It's working fine. May I know what Android version you're testing this on? Have you applied any theme changes in your styles or themes? I'm using `Theme.MaterialComponents.DayNight.DarkActionBar` as my parent theme with no customization apart from colors. – Mayur Gajra Apr 13 '22 at 02:40
  • I am testing on Api 30 and the theme is "Theme.Material3.Light.NoActionBar" and my dialog is not showing as yours. – Gaurav Apr 13 '22 at 10:02
  • Are you using appcompat.app.AlertDialog? – Gaurav Apr 13 '22 at 10:39
  • @Gaurav I'm using `androidx.appcompat.app.AlertDialog` – Mayur Gajra Apr 13 '22 at 10:48
  • I was using 'android.app.AlertDialog' and it was not working but the appcompat alert dialog is working perfectly , is there any way to get the UI as MaterialAlertDialog? – Gaurav Apr 13 '22 at 11:51
  • @Gaurav Sorry, I am not aware of achieving the same with `MaterialAlertDialog`. – Mayur Gajra Apr 13 '22 at 13:40
1

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!

cactustictacs
  • 17,935
  • 2
  • 14
  • 25
  • I tried "androidx.appcompat.app.AlertDialog" instead of "android.app.AlertDialog" and it works just as required which is really great! but I am using MaterialAleartDialog at other places so the theming of appcompat dialog is very different. I have never used appcompat and read that it used with appcompat themes , I am using material3 theme so will it be okay? – Gaurav Apr 13 '22 at 10:37
  • 1
    @Gaurav you need the Material version if you want Material theming - you can mix and match the two (and you are using `appcompat` one way or another, a lot of things have a dependency on it - including the Material library!). But I'm assuming you want your app to look consistent, especially if you're using the new stuff. Honestly, the simplest way is to just let the framework decide how it should look, that's sorta the idea anyway. But it seems if your list items are over 1 line high, it ends up scrolling - and if you don't want that, I think you might need to use `setView` and customise it – cactustictacs Apr 13 '22 at 18:08