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.
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.
CodePudding user response:
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:
CodePudding user response:
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!