Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use View Binding in Custom Dialog box layout?

I am having trouble in implementing view binding in a Custom Dialog Layout. Is it possible?

 private fun showCustomDialog(title: String) {
    val dialog = Dialog(activity)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(false)

    dialog.setContentView(R.layout.custom_layout)

    val body = dialog.findViewById(R.id.body) as TextView
    body.text = title

    val noBtn = dialog.findViewById(R.id.noBtn) as TextView
    yesBtn.setOnClickListener {
        dialog.dismiss()
    }

    val yesBtn = dialog.findViewById(R.id.yesBtn) as Button
    noBtn.setOnClickListener { dialog.dismiss() }
    dialog.show()

}

2 Answers

It is possible.

CustomDialogBinding binding = CustomDialogBinding 
          .inflate(LayoutInflater.from(getContext()));
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setView(binding.getRoot());

Where CustomDialogBinding is the name of the view binding file for your custom layout

kotlin

val bind :CustomDialogBinding = CustomDialogBinding .inflate(inflater)
dialog.setContentView(bind.root)
like image 181
SABANTO Avatar answered Sep 16 '25 00:09

SABANTO


Code:

val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val binding = CustomDialogLayoutBinding.inflate(inflater)
dialog.setContentView(binding.root)
like image 37
Ghayas Avatar answered Sep 16 '25 00:09

Ghayas