I need to display some custome buttons on dialog fragment, but Alert dialog just has .setPositiveButton AND .setNegativeButton
my Alert dialog must be something like
What I had done::
private fun alertDialog() {
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
builder.setIcon(R.drawable.warning)
builder.setTitle("خروج از برنامه ")
builder.setMessage("از برنامه خارج میشوید؟")
.setCancelable(false)
.setPositiveButton("بله",
DialogInterface.OnClickListener { dialog, id -> System.exit(0) })
.setNegativeButton("خیر",
DialogInterface.OnClickListener { dialog, id -> dialog.cancel() })
val alert: AlertDialog = builder.create()
alert.show()
}
and there was something like this on the Internet:
fun withItems(view: View) {
val items = arrayOf("red", "Orange", "Yellow", "Blue")
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
with(builder)
{
setTitle("ارسال گزارش به صورت:")
setItems(items) { dialog, which ->
Toast.makeText(activity, items[which] " is clicked",
Toast.LENGTH_SHORT).show()
}
show()
}
}
but non of them worked as I want
CodePudding user response:
If I'm understanding it right, you need to create a custom layout and give it to your alertDialog such as this:
val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
val dialogView: View = LayoutInflater.from(context).inflate(R.layout.dialog, null, false)
val myFirstButton: Button = dialogView.findViewById(R.id.first_button)
val mySecondButton: Button = dialogView.findViewById(R.id.second_button)
myFirstButton.setOnClickListener { ... }
mySecondButton.setOnClickListener { ... }
builder.setView(dialogView)
.setTitle("MyTitle")
.create()
.show()