0

I have a DialogFragment with multi chose style, which means it does not dismiss on select/deselect. The only way to dismiss it seems to be to click on the device Back button.

Im finding it strange to have click the device back button to dismiss the dialog. Is there a way to add a Done button to the dialog, next to the title?

enter image description here

pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • are you using AlertDialog or What ? – Mohammed Alaa Jan 13 '20 at 01:59
  • @MohammedAlaa yes I am sir. – pnizzle Jan 13 '20 at 02:07
  • I'm currently trying this: https://stackoverflow.com/questions/25530127/place-a-button-inside-dialogfragments-title – pnizzle Jan 13 '20 at 02:08
  • there is an answer that state that you should adjust the layout-prams for button, also you can try other solutions like [link1](https://stackoverflow.com/questions/10932832/multiple-choice-alertdialog-with-custom-adapter?lq=1), [link2](https://stackoverflow.com/questions/52855974/how-to-make-an-alertdialog-with-multichoice-items-along-with-an-edittext) – Mohammed Alaa Jan 13 '20 at 02:13
  • @MohammedAlaa those links just show how to creare multiple chose dialog. My question is on adding a button next to the dialogs main title (at the top). As here: stackoverflow.com/questions/25530127/… – pnizzle Jan 13 '20 at 02:16

1 Answers1

0

Its quite simple actually, I'm extending the theoretical answer here:

Create your custom layout eg as myLayoutTitle.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="50dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/select_type_title_container"
    android:background="#f6f6f6">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        app:layout_constraintLeft_toLeftOf="@+id/select_type_title_container"
        app:layout_constraintTop_toTopOf="@+id/select_type_title_container"
        android:text="@string/menu_types"
        android:layout_gravity ="center_vertical"
        android:gravity="center_vertical"
        android:textStyle="bold"
        android:textSize="20dp"
        android:id="@+id/select_types_textview"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        app:layout_constraintRight_toRightOf="@+id/select_type_title_container"
        app:layout_constraintTop_toTopOf="@+id/select_type_title_container"
        android:layout_margin="5dp"
        android:text="Done"
        android:textColor="@color/colorPrimary"
        android:id="@+id/done_select_types"
        android:layout_gravity ="center_vertical"/>
</androidx.constraintlayout.widget.ConstraintLayout>

The on your dialog builder

builder.setCustomTitle(activity?.layoutInflater?.inflate(R.layout.myLayoutTitle, null))
pnizzle
  • 6,243
  • 4
  • 52
  • 81