Reputation:
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()
}
Upvotes: 22
Views: 18593
Reputation: 358
I would like to add a complementary answer from SABANTO. Hopefully it can help people who are confused about how to get that CustomDialogBinding.
CustomDialogBinding is obtained from the resource name in the layout folder that you want to use for the dialog. For example, if you have a layout named view_printer_dialog.xml
then the binding name would be ViewPrinterDialogBinding
.
You need to import the databinding in order to use it. The syntax to import it is as follows: import your_app_packgage.databinding.binding_name
. For example:
import id.leap.pos.databinding.ViewPrinterDialogBinding;
After that you can use it as follows:
val binding = ViewPrinterDialogBinding.inflate(layoutInflater)
val dialog = Dialog(this)
dialog.setContentView(binding.root)
binding.btnCancel.setOnClickListener {
dialog.dismiss()
}
Example code inside view_printer_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="300dp"
android:orientation="vertical">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:title="Printers"/>
<Button android:layout_gravity="center"
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
Upvotes: 0
Reputation: 2618
I don't have much experience working on Android but I ran into this error and finally I was able to fix it. Regarding your code, I think that should be something like this:
private fun showCustomDialog(title: String) {
val dialog = Dialog(activity)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
val dialogBinding = customLayoutBinding.inflate(layoutInflater)
dialog.setContentView(this.dialogBinding.root)
val body = dialogBinding.body
val noBtn = dialogBinding.noBtn
val yesBtn = dialogBinding.yesBtn
body.text = title
noBtn.setOnClickListener {
dialog.dismiss()
}
yesBtn.setOnClickListener {
dialog.dismiss()
}
dialog.show()
}
Upvotes: 0
Reputation: 31
Code:
val winnerDBinding =WinnerDialougeBinding.inflate(layoutInflater)
val dialog = Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(false)
dialog.setContentView(winnerDBinding.root)
winnerDBinding.btnPlayAgain.setOnClickListener {
val intent = Intent(this,MainActivity::class.java)
finish()
startActivity(intent)
}
dialog.show()
Upvotes: 3
Reputation: 1
Similar way to the first answer, including a few extra lines. Assumes your binding is DialogReviewBinding
.
val inflater = activity.layoutInflater
val dialogBinding = DialogReviewBinding.inflate(inflater)
val dialog = AlertDialog.Builder(activity).create()
dialog.setView(dialogBinding.root)
dialog.show()
And this is surrounded by a activity?.let { activity -> }
block
Upvotes: 0
Reputation: 489
Example:
val dialogBinding = DialogCustomBinding.inflate(layoutInflater)
dialog.setView(dialogBinding.root)
Upvotes: 4
Reputation: 1324
Code:
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val binding = CustomDialogLayoutBinding.inflate(inflater)
dialog.setContentView(binding.root)
Upvotes: 14
Reputation: 1506
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)
Upvotes: 47