Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting maximum expanded height for bottomsheet dynamically

How to set maximum expanded height in android support design bottom sheet?

The question is an extension to the above question, i want to set the max expanded height of the sheet but dynamically according to the screen size.

I have tried setting new layout params to the view implementing bottomsheet behaviour but it does nothing good.

like image 329
saurabh dhillon Avatar asked Mar 17 '18 08:03

saurabh dhillon


2 Answers

Please use this and chill :)

  1. const val BOTTOMSHEET_HEIGHT_TO_SCREEN_HEIGHT_RATIO = 0.80 //change according to your requirement
  2. override onCreateDialog() in your bottomsheetFragment

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
        dialog.setOnShowListener {
            dialog.findViewById<FrameLayout>(com.google.android.material.R.id.design_bottom_sheet)
                ?.apply {
                    val maxDesiredHeight =
                        (resources.displayMetrics.heightPixels * BOTTOMSHEET_HEIGHT_TO_SCREEN_HEIGHT_RATIO).toInt()
                    if (this.height > maxDesiredHeight) {
                        val bottomSheetLayoutParams = this.layoutParams
                        bottomSheetLayoutParams.height = maxDesiredHeight
                        this.layoutParams = bottomSheetLayoutParams
                    }
                    BottomSheetBehavior.from(this)?.apply {
                        this.state = BottomSheetBehavior.STATE_EXPANDED
                        this.skipCollapsed = true
                    }
                }
        }
        return dialog
    }
like image 73
Kapil Sachan Avatar answered Sep 18 '22 14:09

Kapil Sachan


2021 I'm late but someone will need Kotlin extenxion:

fun View.setupFullHeight(maxHeight: Double = 0.3) {
val displayMetrics = context?.resources?.displayMetrics
val height = displayMetrics?.heightPixels
val maximalHeight = (height?.times(maxHeight))?.toInt()
val layoutParams = this.layoutParams
maximalHeight?.let {
    layoutParams.height = it
}
this.layoutParams = layoutParams

}

How to use:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return object : BottomSheetDialog(requireContext(), R.style.DialogRoundedCornerStyle) {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)

            dialog?.setOnShowListener {
                val bottomSheetDialog = it as BottomSheetDialog
                val parentLayout =
                    bottomSheetDialog.findViewById<View>(R.id.design_bottom_sheet)
                parentLayout?.let { view ->
                    val behavior = BottomSheetBehavior.from(view)
                    view.setupFullHeight()
                    behavior.apply {
                        state = BottomSheetBehavior.STATE_EXPANDED
                        isDraggable = false
                        isCancelable = false
                    }
                }
            }
        }

        override fun onBackPressed() {
            super.onBackPressed()
            dialog?.dismiss()
        }
    }
}
like image 44
Романыч Avatar answered Sep 17 '22 14:09

Романыч