Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use View Binding on BottomSheetDialog

I'm trying to make a BottomSheetDialog inside my MainActivity, but right now I'm using synthetic to bind my views. But now I'm stuck with how to attach ViewBinding to my BottomSheetDialog.

Here's what I do, using synthetic.

dialog = BottomSheetDialog(this, R.style.BottomDialogStyle)
dialogView = LayoutInflater.from(this).inflate(R.layout.test_dialog, dialog.mainContainer, false)
dialog.setContentView(dialogView)

And here is what's confusing me with ViewBinding

dialog = BottomSheetDialog(this, R.style.BottomDialogStyle)
binding = TestDialogBinding.inflate(?)
dialog.setContentView(binding.root)

From my example above, I'm wondering what I should fill the parameters with, because unlike in an activity where I could just fill that parameter with layoutInflater or in a RecyclerView Adapter where I cant fill that parameter with

LayoutInflater.from(parent.context), parent, and false.

like image 410
Josh Adrian Avatar asked Apr 20 '21 04:04

Josh Adrian


People also ask

How do you bind dialog?

It is possible to use databinding in a Dialog, first to get the binding working on your Dialog you should inflate it first and pass it to the setContentView like this. DialogOlaBookingConfirmedBinding binding = DataBindingUtil. inflate(LayoutInflater. from(getContext()), R.

What is BottomSheetDialogFragment?

↳ com.google.android.material.bottomsheet.BottomSheetDialogFragment. Modal bottom sheet. This is a version of DialogFragment that shows a bottom sheet using BottomSheetDialog instead of a floating dialog.


2 Answers

You also can create a LayouInflater :

val inflater = LayoutInflater.from(requireContext())

then pass inflater to :

binding = TestDialogBinding.inflate(inflater)
like image 96
NhatVM Avatar answered Oct 20 '22 14:10

NhatVM


For Binding follow as mentioned :

 bottomSheetDialog = BottomSheetDialog(mContext, R.style.BottomSheetDialogStyle)
 mBottomSheetBinding = TestDialogBinding.inflate(layoutInflater, null, false)

 bottomSheetDialog.setContentView(mBottomSheetBinding!!.root)
 bottomSheetDialog.show()
like image 2
Mohini Thakkar Avatar answered Oct 20 '22 14:10

Mohini Thakkar