MadMax
MadMax

Reputation: 101

Custom Dialog not showing with certain XML

I'm trying to display a custom dialog but it's not showing up but making the background transparent. The weirdest thing is that when i change just the custom dialog xml it shows up without problems so i think it could be the xml but i've been looking for a while and i can't find the problem.

This is the fragment code:

class DeleteConfirmationFragment : Fragment() { 
private lateinit var binding: FragmentDeleteConfirmationBinding
lateinit var dialog : Dialog

  override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_delete_confirmation, container, false)
    }

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding = FragmentDeleteConfirmationBinding.bind(view)

        dialog = context?.let { Dialog(it) }!!
        binding.btnNo.setOnClickListener {
            openDeletedAccountDialog()
        }
    }

    private fun openDeletedAccountDialog() {
        dialog.setContentView(R.layout.dialog_deleted_account) // if i change this xml file for other it 
        //works properly so i think it could be an android bug..

        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.show()
    }
}

This is the xml that isnt working:

<?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="wrap_content"
    android:layout_gravity="center"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp">


    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewDeletedAccount"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:cardCornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:id="@+id/tvDeletedAccount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:fontFamily="@font/poppins_semibold"
                android:text="Cuenta eliminada"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.496"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:fontFamily="@font/poppins_semibold"
                android:gravity="center_horizontal"
                android:padding="10dp"
                android:text="Tu cuenta se ha eliminado correctamente.  ¡Esperamos volver a verte pronto!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvDeletedAccount"
                app:layout_constraintVertical_bias="0.0" />


        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

Display using custom xml which isnt working:

enter image description here

Other custom dialog xml code that is working properly:

<?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="wrap_content"
    android:layout_gravity="center"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp">

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/poppins_bold"
        android:gravity="center"
        android:text="¡Tu friendzone ha crecido!"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/cardViewFriendzone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.884" />

    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewFriendzone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <androidx.cardview.widget.CardView
                android:id="@+id/cardView2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                app:cardCornerRadius="100dp"
                app:cardElevation="20dp"
                android:layout_marginTop="20dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.47"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <ImageView
                    android:id="@+id/tvNotificationImage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="centerCrop"
                    app:srcCompat="@drawable/pedra" />
            </androidx.cardview.widget.CardView>

            <TextView
                android:id="@+id/tvDialogName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:fontFamily="@font/poppins_semibold"
                android:text="María González"
                android:textColor="@color/black"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.496"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/cardView2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:fontFamily="@font/poppins_semibold"
                android:padding="10dp"
                android:text="Soy una chica aventurera, amistosa y muy curiosa. Mi pasión son los animales, en especial los perros."
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/tvDialogName"
                app:layout_constraintVertical_bias="0.45" />


        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

If i change just the xml in the open deleted dialog function like this it shows up the other dialog correctly:

    private fun openDeletedAccountDialog() {
        dialog.setContentView(R.layout.dialog_friendzone_layout) // <-- change made here
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.show()
    }

Display using other custom dialog xml:

enter image description here

Any idea of what is happening?

Upvotes: 0

Views: 101

Answers (2)

Mahdi Roohandeh
Mahdi Roohandeh

Reputation: 1

you just need to change android:layout_width="0dp" of your CardView to android:layout_width="match_parent".

Upvotes: 0

Hamid Sj
Hamid Sj

Reputation: 1023

I had the same problem too and it was because I was using 0dp in ConstrintLayout in order to using match_constraint size but it's not working in dialogs.

Upvotes: 1

Related Questions