How to use View Binding on BottomSheetDialog

7.8k Views Asked by At

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.

4

There are 4 best solutions below

2
On BEST ANSWER

You also can create a LayouInflater :

val inflater = LayoutInflater.from(requireContext())

then pass inflater to :

binding = TestDialogBinding.inflate(inflater)
0
On

For Binding follow as mentioned :

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

 bottomSheetDialog.setContentView(mBottomSheetBinding!!.root)
 bottomSheetDialog.show()
0
On
val view = PostShareSheetDialogBinding.inflate(layoutInflater, null, false)
val dialog = BottomSheetDialog(it, R.style.AppBottomSheetDialogTheme)

at the end of dialog

dialog.setContentView(view.root)
dialog.show()

add this into style.xml

<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
    <item name="android:backgroundDimEnabled">true</item>
</style>

<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/bottomsheet_rounded_background</item>
</style>

And rounded corner shape as well.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white"/>
    <corners android:topLeftRadius="@dimen/margin_large"
        android:topRightRadius="@dimen/margin_large"/>
</shape>
0
On

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)
}