Skip to content

Instantly share code, notes, and snippets.

@qamarelsafadi
Last active August 2, 2023 04:56
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save qamarelsafadi/a880dbe16645687aded775b3763424a9 to your computer and use it in GitHub Desktop.
Save qamarelsafadi/a880dbe16645687aded775b3763424a9 to your computer and use it in GitHub Desktop.
This class is a generic class for BottomSheetDialogFragment using Kotlin, it will helps you to use one class for all ur bottom sheets on your app.
/*
* Copyright (c) 2022. Qamar A. Safadi
*/
package com.qamar.test.base
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.Toast
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.qamar.test.R
open class BaseBottomSheet(
@LayoutRes private val layoutRes: Int? = 0,
private val corneredTheme: Int? = R.style.ThemeOverlay_MaterialComponents_BottomSheetDialog,
private val normalTheme: Int? = R.style.ThemeOverlay_MaterialComponents_BottomSheetDialog,
private val isCornered: Boolean? = false,
private val onBind: (View, binding: ViewDataBinding) -> Unit,
) : BottomSheetDialogFragment() {
//REMINDER : your layout must have data binding syntax <layout> </layout> to avoid null exception...
lateinit var binding: ViewDataBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (isCornered == true) {
setStyle(STYLE_NORMAL, corneredTheme!!)
} else setStyle(STYLE_NORMAL, normalTheme!!)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
try {
return if (layoutRes == 0) {
dismiss()
null
} else {
binding = DataBindingUtil.inflate(inflater, layoutRes!!, container, false)
dialog?.setOnShowListener { dialog ->
val sheetDialog = dialog as BottomSheetDialog
val bottomSheet =
sheetDialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
setupHeight(bottomSheet)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
bottomSheetBehavior.isFitToContents = true
bottomSheetBehavior.isDraggable = true
bottomSheetBehavior.isHideable = true
}
binding.root
}
} catch (e: Exception) {
Toast.makeText(
context,
"Opps,You forget to convert ur layout to data binding layout ^_^",
Toast.LENGTH_LONG
).show()
return null
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
onBind(view, binding)
}
private fun setupHeight(bottomSheet: View) {
val layoutParams = bottomSheet.layoutParams
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
bottomSheet.layoutParams = layoutParams
}
}
private fun bindChangePasswordSheet() {
val changePasswordSheet = BaseBottomSheet(
layoutRes = R.layout.change_password_bottom_sheet,
corneredTheme = R.style.AppBottomSheetDialogTheme,
isCornered = true,
onBind = { view, _binding ->
val bind = _binding as ChangePasswordBottomSheetBinding
with(bind) {
// do what you want
}
})
changePasswordSheet.show(childFragmentManager, "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment