Michalodzien
Michalodzien

Reputation: 35

I want to make a layout transparent but i cant figure out how to

I want to make my root view transparent, if you look at the pictures you should understand what i want to do. As you can see i have tried setting the background to transparent, it dont work and i have tried doing it in the program too.

this

enter image description here

<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:background="@android:color/transparent"
tools:ignore="ContentDescription">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:elevation="15dp"
    app:cardCornerRadius="15dp"
    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"

        android:background="@color/white"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/dialog_simple_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="12dp"
            android:fontFamily="sans-serif-light"

            android:textColor="@color/textSecondary"
            android:textSize="@dimen/text_medium"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="hello" />

        <TextView
            android:id="@+id/dialog_simple_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"

            android:fontFamily="sans-serif-light"
            android:textColor="@color/textSecondary"
            android:textSize="@dimen/text_regular"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/dialog_simple_title"
            tools:text="hello" />


        <ImageButton
            android:id="@+id/dialog_simple_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"

            android:layout_marginEnd="16dp"
            android:background="@color/white"
            android:src="@drawable/ic_x"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.cardview.widget.CardView
            android:id="@+id/dialog_simple_done"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginStart="15dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="15dp"
            android:layout_marginBottom="20dp"
            android:backgroundTint="@color/colorPrimary"
            app:cardCornerRadius="50dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"

            app:layout_constraintTop_toBottomOf="@+id/dialog_simple_description">


            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="?selectableItemBackground"
                app:srcCompat="@drawable/ic_done">
            </ImageButton>
        </androidx.cardview.widget.CardView>
    </androidx.constraintlayout.widget.ConstraintLayout>

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

I have also tried setting dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent) in my dialog like this but it doesn't work still

class SimpleAlertDialog: DialogFragment() {
lateinit var dialogView: View
lateinit var simpleAlertError: SimpleAlertError

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val dialog = super.onCreateDialog(savedInstanceState)
    dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)

    return activity?.let {
        val builder = AlertDialog.Builder(it)
        val inflater = requireActivity().layoutInflater;
        dialogView = inflater.inflate(R.layout.dialog_simple_alert, null)
        builder.setView(dialogView)
        getErrorFromArgs()
        setDialogData()
        builder.create()
    } ?: throw IllegalStateException("Activity cannot be null")

}

private fun setDialogData() {
    dialogView.dialog_simple_title.text = simpleAlertError.title
    dialogView.dialog_simple_description.text = simpleAlertError.description
}

fun getErrorFromArgs(){
   simpleAlertError = arguments?.getParcelable<SimpleAlertError>("error")!!
}

}

Upvotes: 0

Views: 447

Answers (1)

sourabhgupta811
sourabhgupta811

Reputation: 106

You are creating an AlertDialog but you never made its window transparent. Use-

return activity?.let {
    val builder = AlertDialog.Builder(it)
    val inflater = requireActivity().layoutInflater;
    dialogView = inflater.inflate(R.layout.dialog_simple_alert, null)
    builder.setView(dialogView)
    getErrorFromArgs()
    setDialogData()

    // this is the dialog that you need to make transparent
    val dialog = builder.create()
    dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)


} ?: throw IllegalStateException("Activity cannot be null")

Upvotes: 2

Related Questions