9

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.

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
Josh Adrian
  • 126
  • 1
  • 9

4 Answers4

12

You also can create a LayouInflater :

val inflater = LayoutInflater.from(requireContext())

then pass inflater to :

binding = TestDialogBinding.inflate(inflater)
NhatVM
  • 1,964
  • 17
  • 25
  • I tried using ```requireContext()```, but that function is not available in my MainActivity. Is it okay if I just use ```dialog.layoutInflater``` instead? – Josh Adrian Apr 20 '21 at 04:19
  • If you are initializing ViewBinding in a Activity, you just need put this ( your MainActity) into LayoutInflater.from(this); – NhatVM Apr 20 '21 at 04:22
3

For Binding follow as mentioned :

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

 bottomSheetDialog.setContentView(mBottomSheetBinding!!.root)
 bottomSheetDialog.show()
Mohini Thakkar
  • 166
  • 3
  • 7
0

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)
}
Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36
0
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>