I am posting the kotlin code that I am using and it works fine for me. you can also set click listener for dialog buttons.
this is my XML code:
layout_custom_alert_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layoutDirection="ltr"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/view6"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/colorPrimary" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/view6"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp">
<TextView
android:id="@+id/txt_alert_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
tools:text="are you sure?"
android:textAlignment="center"
android:textColor="@android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_alert_positive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_marginTop="8dp"
android:background="@android:color/transparent"
tools:text="yes"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn_alert_negative"
app:layout_constraintTop_toBottomOf="@+id/txt_alert_title" />
<Button
android:id="@+id/btn_alert_negative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@android:color/transparent"
tools:text="no"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintEnd_toStartOf="@+id/btn_alert_positive"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_alert_title" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
mAlertDialog.kt
class mAlertDialog(context: Context) {
private val btn_positive : Button
private val btn_negative : Button
private val txt_alert_title : TextView
private val dialog : AlertDialog
init {
val view = LayoutInflater.from(context).inflate(R.layout.layout_custom_alert_dialog,null)
val dialog_builder = AlertDialog.Builder(context)
dialog_builder.setView(view)
btn_negative = view.findViewById(R.id.btn_alert_negative)
btn_positive = view.findViewById(R.id.btn_alert_positive)
txt_alert_title = view.findViewById(R.id.txt_alert_title)
dialog = dialog_builder.create()
}
fun show()
{
dialog.show()
}
fun setPositiveClickListener(listener :onClickListener)
{
btn_positive.setOnClickListener { v ->
listener.onClick(btn_positive)
dialog.dismiss()
}
}
fun setNegativeClickListener(listener: onClickListener)
{
btn_negative.setOnClickListener { v ->
listener.onClick(btn_negative)
dialog.dismiss()
}
}
fun setPoitiveButtonText(text : String)
{
btn_positive.text = text
}
fun setNegativeButtonText(text : String)
{
btn_negative.text = text
}
fun setAlertTitle(title : String)
{
txt_alert_title.text = title
}
}
interface for click listeners:
onClickListener.kt
interface onClickListener{
fun onClick(view : View)
}
Sample Usage
val dialog = mAlertDialog(context)
dialog.setNegativeButtonText("no i dont")
dialog.setPoitiveButtonText("yes is do")
dialog.setAlertTitle("do you like this alert dialog?")
dialog.setPositiveClickListener(object : onClickListener {
override fun onClick(view: View) {
Toast.makeText(context, "yes", Toast.LENGTH_SHORT).show()
}
})
dialog.setNegativeClickListener(object : onClickListener {
override fun onClick(view: View) {
Toast.makeText(context, "no", Toast.LENGTH_SHORT).show()
}
})
dialog.show()
I hope, this will help you!