Reputation: 29
I have added the bottom sheet dialog on a fragment.
Right now I can't open it.
Whenever I click on the show button it doesn't work at all.
How can I fix this problem?
CreateFragment.kt
class CreateFragment : Fragment() {
lateinit var binding: FragmentCreateBinding;
val viewModel: NotesViewModel by viewModels()
private var color = -1
private val currentDate = SimpleDateFormat.getInstance().format(Date())
private lateinit var result: String
private val job = CoroutineScope(Dispatchers.Main)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentCreateBinding.inflate(layoutInflater, container, false)
val animation = MaterialContainerTransform().apply {
drawingViewId = R.id.createFragment
scrimColor = Color.TRANSPARENT
duration = 300L
}
binding.backbutton.setOnClickListener {
requireView().hideKeyboard()
Navigation.findNavController(it).popBackStack()
}
sharedElementEnterTransition = animation
sharedElementReturnTransition = animation
binding.fabcolorpick.setOnClickListener {
val bottomSheetDialog = BottomSheetDialog(
requireContext(),
R.style.BottomSheetDialogTheme
)
val bottomSheetView: View = layoutInflater.inflate(
R.layout.bottomsheetlayout,
null,
)
val bottomSheetBinding = BottomsheetlayoutBinding.bind(bottomSheetView)
bottomSheetBinding.apply {
colorpicker.apply {
setSelectedColor(color)
setOnColorSelectedListener { value ->
color = value
binding.apply {
createFragmentxmlid.setBackgroundColor(color)
toolbarfragmentnotecontent.setBackgroundColor(color)
bottombar.setBackgroundColor(color)
activity?.window?.statusBarColor = color
}
bottomSheetBinding.bottomSheetparent.setCardBackgroundColor(color)
}
}
bottomSheetparent.setCardBackgroundColor(color)
}
bottomSheetView.post {
bottomSheetDialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
binding.btndonenotes.setOnClickListener {
createNotes(it)
}
try {
binding.edittextnote.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
binding.bottombar.visibility = View.VISIBLE
binding.edittextnote.setStylesBar(binding.styleBar)
} else {
binding.bottombar.visibility = View.GONE
}
}
} catch (e: Throwable) {
Log.d("TAG", e.stackTraceToString())
}
return binding.root
}
Upvotes: 0
Views: 1085
Reputation: 134
this is how I would do it
// the content view
val bottomSheetView: View = layoutInflater.inflate(
R.layout.bottomsheetlayout,
null,
)
// and the dialog
val bottomSheetDialog = BottomSheetDialog( requireContext(),R.style.BottomSheetDialogTheme).apply{
setContent(bottomSheetView)
// or if I am using viewBinding
setContent(bottomSheetBinding.root)
}
// then in button's onClick
button.setOnClickListener{
bottomSheetDialog.show()
}
Upvotes: 0