Add a dependency code to your module's build.gradle
file.
buildFeatures {
viewBinding true
}
button background
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="35dp" />
<stroke android:width="2dp" android:color="#E10B0B" />
<solid android:color="#E10B0B" />
</shape>
view binding
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
//view binding initialize
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
}
show button clicked
binding.showBtn.setOnClickListener {
// show button is clicked
val alertDialogBuilder = AlertDialog.Builder(this)
//inflate dialog_tmp.xml
val inflater = layoutInflater
var dialogView = inflater.inflate(R.layout.dialog_tmp, null)
alertDialogBuilder.setView(dialogView) // alert dialog set View
//create alert dialog
val alertDialog: AlertDialog = alertDialogBuilder.create()
alertDialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
alertDialog.setCancelable(false) // When we press the back button, we prevent it from closing
alertDialog.setCanceledOnTouchOutside(false)
alertDialog.show()
val closeButton: Button = dialogView.findViewById(R.id.closeButton)
closeButton.setOnClickListener {
alertDialog.cancel()
}
}