How to use View Binding on BottomSheetDialog

Viewed 4245

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.

3 Answers

You also can create a LayouInflater :

val inflater = LayoutInflater.from(requireContext())

then pass inflater to :

binding = TestDialogBinding.inflate(inflater)

For Binding follow as mentioned :

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

 bottomSheetDialog.setContentView(mBottomSheetBinding!!.root)
 bottomSheetDialog.show()

In your BottomSheetDialog class define binding variable:

private var binding: BottomSheetDatePickerBinding

Then you can override init

init {
    binding = BottomSheetDatePickerBinding.inflate(
        LayoutInflater.from(context)
    )
    setContentView(binding.root)
}
Related