Sitemap

Leverage Material Bottom Sheet in The Right Way

3 min readNov 16, 2020

--

Press enter or click to view image in full size
source: material.io/components/sheets-bottom

Bottom sheet is a component that slides up from the bottom of the screen to reveal more content. You can find out a more detailed Bottom Sheet guideline on this. There are so many use cases using the bottom sheet out there but in this article, I will show you how to make a more generic purpose bottom sheet.

Adding libraries

First of all, you have to add a Bottom Sheet library and App Compat. In this case, I am using the app-compat library from androidx package. You can replace the version of the library with the latest one.

implementation 'androidx.appcompat:appcompat:X.X.X'
implementation 'com.google.android.material:material:X.X.X'

Making a generic Bottom Sheet

First, you have to make an Activity container to place your Bottom Sheet.

class BottomSheetActivity : AppCompatActivity, SheetActions

I have created a custom callback to listen to the actions and add a custom result to gathering data from the Bottom Sheet.

interface SheetActions {    fun onSheetResult(identifier: String, result: SheetResult)
}

Get Rahmat Rasyidi Hakim’s stories in your inbox

Join Medium for free to get updates from this writer.

class SheetResult : Serializable { var data: Bundle? = null
}

Second, implementing a base class Bottom Sheet.
You can customize style from the bottom sheet with your theme by replacing the method getTheme() and I have created an abstract class SheetBehaviour that you can override from your Bottom Sheet’s implementation.

abstract class Sheet(private val contentRes: Int) : BottomSheetDialogFragment(),
SheetBehaviour {

override fun getTheme(): Int = R.style.BottomSheetDialogTheme

override val peekHeight: PeekHeight
get() = PeekHeight.HEIGHT_WRAP
override val isCancellable: Boolean
get() = true
override val isDraggable: Boolean
get() = true
override val identifier: String
get() = this::class.java.simpleName

private var onSheetActions: SheetActions? = null
private val sheetResult = SheetResult()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(contentRes, container, false)
}

override fun onAttach(context: Context) {
super.onAttach(context)
if (context is SheetActions) {
onSheetActions = context
}
}

override fun onDetach() {
onSheetActions = null
super.onDetach()
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val dialog = (requireDialog() as BottomSheetDialog)
setPeekHeight(dialog)
dialog.dismissWithAnimation = true
dialog.behavior.isDraggable = isDraggable
dialog.behavior.isHideable = isCancellable
dialog.behavior.saveFlags = BottomSheetBehavior.SAVE_ALL
dialog.behavior.addBottomSheetCallback(bottomSheetCallback)

requireDialog().setCancelable(isCancellable)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return BottomSheetDialog(requireContext(), theme)
}

private fun setPeekHeight(dialog: BottomSheetDialog) {
when (peekHeight) {
PeekHeight.HEIGHT_WRAP -> {
dialog.behavior.isFitToContents = true
}
PeekHeight.HEIGHT_FULL -> {
dialog.behavior.state = BottomSheetBehavior.STATE_EXPANDED
dialog.behavior.isFitToContents = false
val lp = (view?.parent as? ViewGroup)?.layoutParams
lp?.height = getDeviceHeight() - 24.dp
}
}
}

private fun setPeekHeight(dialog: BottomSheetDialog, maxHeight: Int) {
dialog.behavior.isFitToContents = false
dialog.behavior.setPeekHeight(maxHeight, true)
(view?.parent as? ViewGroup)?.layoutParams?.height = maxHeight
}

protected fun setResult(bundle: Bundle) {
sheetResult.data = bundle
onSheetActions?.onSheetResult(identifier, sheetResult)
}
private fun getDeviceHeight(): Int {
val displayMetrics = requireContext().resources.displayMetrics
return displayMetrics.heightPixels
}

enum class PeekHeight {
HEIGHT_WRAP,
HEIGHT_FULL
}
}

Third, implementing a custom Bottom Sheet.

class YourCoolSheet : Sheet(R.layout.sheet_cool) {

override val identifier: String get() = IDENTIFIER
override val isCancellable: Boolean get() = true
override val peekHeight: PeekHeight get() = PeekHeight.HEIGHT_WRAP

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btnCool.setOnClickListener {
setResult(Bundle())
dismiss()
}
}

companion object {
const val IDENTIFIER = "coolSheet"
}
}

If you want to catch the sheet result, you can get it on onSheetResult callback from the Activity container.

More resources

That’s all, now you can leverage using your custom bottom sheet with concise and less repetitive code. You can find the gist here, If you have any questions, thoughts, or suggestions please comment below!

--

--

Rahmat Rasyidi Hakim
Rahmat Rasyidi Hakim

Written by Rahmat Rasyidi Hakim

Android Developer and Kotlin Enthusiast