enjektor0
enjektor0

Reputation: 543

Android - Make the background part of the Dialog available

I am creating an AlertDialog as follows.

However, there is a problem. Dialog background cannot be clicked. How do I make the background available while the dialog remains on the screen? Also, I don't want the dialog to be closed when the user is using the background.

TestBannerDialog.kt

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.CountDownTimer
import android.view.Gravity
import android.view.View
import android.view.WindowManager

class TestBannerDialog(context: Context) : Dialog(context, false, null) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.test_banner_dialog)
        setCanceledOnTouchOutside(false)

        window?.let {
            it.attributes.gravity = Gravity.BOTTOM
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }
       
    }
}

test_banner_dialog.xml

<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="wrap_content"
    android:layout_height="80dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="24dp">

    <ImageView ....... />

    <TextView ....... />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Views: 112

Answers (1)

This way you can use something and then add it to the layout of the screen you want it to appear.

class TestBannerView @JvmOverloads constructor(context: Context,
                                                          attrs: AttributeSet? = null,
                                                          defStyle: Int = 0,
                                                          defStyleRes: Int = 0) : FrameLayout(context, attrs, defStyle, defStyleRes) {
init {
    LayoutInflater.from(context).inflate(R.layout.test_banner_view, this, true)
     }
}

Upvotes: 1

Related Questions