0

I am trying to show a custom dialog from a fragment. When it is about to be displayed the screen gets darker, but no dialog is presented. I also tried to show it from the activity containing the fragment, but I get the same result.

Here is the layout for my dialog:

<?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"
    android:layout_width="match_parent"
    android:layout_height="239dp"
    android:background="#00f"
    >

    <androidx.constraintlayout.widget.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"
        android:backgroundTint="#00ff00"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

This is my dialog fragment class:

class AddPlaylistPopUp: DialogFragment(){

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.add_playlist_popup_layout, container, true)
    }
}

and this is the method that should open the dialog in the activity:

   fun testBut(view: View) {
        val fm: FragmentManager = supportFragmentManager
        val editNameDialogFragment = AddPlaylistPopUp()
        editNameDialogFragment.show(fm, "fragment_edit_name")
    }

in the fragment I tried like this:

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

This is the guide I followed: https://guides.codepath.com/android/Using-DialogFragment#passing-data-to-parent-fragment

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

1 Answers1

0

You are not creating any Dialog, you should be overriding onCreateDialog instead.

class AddPlaylistPopUp: DialogFragment(){

    override fun onCreateDialog(
        savedInstanceState: Bundle?
    ): Dialog {

        val builder = AlertDialog.Builder(getContext())
        builder.setTitle("Title")
        builder.setView(R.layout.add_playlist_popup_layout)

        return builder.create()
    }
}

This way you don't event need to implement onCreateView(LayoutInflater, ViewGroup, Bundle) and you have much more control on the dialog creation.

If you would like to stick with onCreateView approach, you could fix your issue by changing the third parameter of function call LayoutInflater.inflate(resource, root, attachToRoot) by setting it to false.

EDIT: To keep match_parent width, override Dialog size.

fillobotto
  • 3,698
  • 5
  • 34
  • 58
  • attachToRoot = false, had no effect unfortunately. I tried the onCreateDialog, but it kinda looks stupid. As you see in my dialog layout the dialog has width = match parent, but I get some margins on the sides and you see there is supposed to be some extra space under the nested constraint layout, but it is cropped out. – Dinu Nicolae Mar 28 '20 at 11:08
  • 1
    @DinuNicolae As of docs: "_A fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state._". You are not creating any Dialog in your code. Maybe `DialogFragment` is not suitable for you use case? See edits – fillobotto Mar 28 '20 at 13:05
  • I just want a pop up that is 100% combustible. What is the best way to achieve that? – Dinu Nicolae Mar 28 '20 at 13:32
  • @DinuNicolae Well, an AlertDialog is usually the recommended way to go. Other possible ways depend only on your specific use case and it's up to you. Please read docs to understand how much you can theme and customize an AlertDialog – fillobotto Mar 28 '20 at 16:45
  • thank you for your time. I have decided to take another approach. I just used the simple dialog and I could get it done the way I want it. – Dinu Nicolae Mar 31 '20 at 15:37