0

I try to create a custom DialogFragment. The problem is it does not look the way I want it to be. I created a layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="239dp"
    android:background="#00f">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/pop_up_shape"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

and it looks like this: enter image description here

I created a class:

class AddPlaylistPopUp: DialogFragment(){

    override fun onCreateDialog(
        savedInstanceState: Bundle?
    ): Dialog {
        val builder = AlertDialog.Builder(context!!)
        builder.setView(R.layout.add_playlist_popup_layout)
        return builder.create()
    }
}

and I present it like this:

fun openDialog(){
    val fm = fragmentManager!!
    val editNameDialogFragment = AddPlaylistPopUp()
    editNameDialogFragment.show(fm, "fragment_edit_name")
}

this is the result:

enter image description here

as you see the pop up looks not like the custom layout I created... What am I doing wrong? There are unnecessary side margins, the green view is bigger and the button is inside the green view. I tried many things but nothing worked. How do I make it look 100% like my layout?

Dinu Nicolae
  • 1,019
  • 2
  • 17
  • 42

1 Answers1

0

I found a solution. Dialog can be used instead of DialogFragment.

class AddPlaylistPopUp(context: Context, themeResId: Int) : Dialog(context, themeResId) {

    init {
        window?.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.add_playlist_popup_layout)
        popUpContainer.setOnClickListener { dismiss() }
        popUpCard.setOnClickListener {}
    }
}

it can be used like this:

private fun openDialog(){
        val addPlaylistPopUp = AddPlaylistPopUp(this.context!!, R.style.DialogAlertTheme)
        addPlaylistPopUp.show()
    }

this is the style:

<!-- Dialog theme -->
    <style name="DialogAlertTheme">
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

I also changed the layout a bit:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/popUpContainer"
    >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="239dp"
        android:background="#00f"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/popUpBackground"
        >

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/popUpCard"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:background="@drawable/pop_up_shape"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:focusable="false"
            android:clickable="false"
            >

        </androidx.constraintlayout.widget.ConstraintLayout>

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dp"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

The result is just as I want it to be: enter image description here

Dinu Nicolae
  • 1,019
  • 2
  • 17
  • 42