Add an extra button in AlertDialog in DialogFragment

133 Views Asked by At

I have a custom DialogFragment, and was trying to use AlertDialog in onCreateDialog(). The AlertDialog only has title, singleChoiceItem, positiveButton, etc. But I would like to add an extra button on the top left to be a back button that I could add a onClickListener. How could I do that? Thanks!

2

There are 2 best solutions below

0
Rahim Virani On

You can create your own xml file with a button on left corner.Now that you have created an Xml file as Custom layout. inflate that file using below code

 AlertDialog.Builder builder
        = new AlertDialog.Builder(this);
    builder.setTitle("Name");

    // set the custom layout
    final View customLayout
        = getLayoutInflater()
              .inflate(
                  R.layout.custom_layout,
                  null);
    builder.setView(customLayout);

After creating your own custom layout set a listener for the button that you have positioned on top left corner in xml by calling findViewByID on Dialog and then calling closeButton.setOnClickListener.

0
Affan Ahmed On

You can use custom dialog. Your Kotlin Class:

 class CustomDialog(var c: Activity) : Dialog(c), View.OnClickListener {
    var d: Dialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.dailog_custom)
    }

    override fun onClick(v: View) {

    }
}

XML file of dialog:

        <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_marginHorizontal="@dimen/extra_padding"
        android:background="@drawable/bg_ripple_secondary_variant_less_rounded"
        android:padding="20dp">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:textStyle="bold"
            android:textSize="@dimen/_12sdp"
            android:textColor="@color/red"
            android:text="Title" />

        <TextView
            android:id="@+id/tvMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/less_padding"
            app:layout_constraintTop_toBottomOf="@id/tvTitle"
            app:layout_constraintStart_toStartOf="parent"
            android:text="Your Message"
            android:textStyle="bold"
            android:textSize="@dimen/_12sdp"
            android:textColor="?attr/colorOnPrimary" />

        <TextView
            android:id="@+id/btnYes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/tvMessage"
            app:layout_constraintEnd_toEndOf="parent"
            android:padding="@dimen/fab_padding"
            android:textColor="@color/red"
            android:text="@string/yes" />

        <TextView
            android:id="@+id/btnNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/tvMessage"
            app:layout_constraintEnd_toStartOf="@id/btnYes"
            android:padding="@dimen/fab_padding"
            android:textColor="?attr/colorOnPrimary"
            android:text="@string/no" />

    </androidx.constraintlayout.widget.ConstraintLayout>