Home > OS >  Add an extra button in AlertDialog in DialogFragment
Add an extra button in AlertDialog in DialogFragment

Time:11-11

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!

CodePudding user response:

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.

CodePudding user response:

You can use custome dialoge. Your Kotling 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:

        <?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>
  • Related