Untitled
unknown
plain_text
2 years ago
3.1 kB
9
Indexable
class SelectCarBottomSheetDialogFragment : BottomSheetDialogFragment() {
lateinit var binding: BottomSheetDialogBinding
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = BottomSheetDialogBinding.inflate(inflater, container, false)
return binding.root
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
val statusBarHeight = WindowCompat.getInsetsController(
dialog.window!!,
dialog.window!!.decorView
).systemBarsBehavior
val windowBackground = InsetDrawable(
ColorDrawable(Color.TRANSPARENT),
0,
16F.dp(requireContext()),
0,
0
)
dialog.window?.setBackgroundDrawable(windowBackground)
Handler(Looper.getMainLooper()).postDelayed({
dialog.behavior.apply {
state = BottomSheetBehavior.STATE_EXPANDED
isDraggable = true
skipCollapsed = true
}
dialog.dismissWithAnimation = true
}, 200)
dialog.setOnShowListener {
val bottomSheetDialog = it as BottomSheetDialog
val materialBottomSheetId = com.google.android.material.R.id.design_bottom_sheet
val parentLayout = bottomSheetDialog.findViewById<View>(materialBottomSheetId)
parentLayout?.let {
val behaviour = BottomSheetBehavior.from(it)
behaviour.state = BottomSheetBehavior.STATE_EXPANDED
behaviour.peekHeight = getHeightPx(resources.displayMetrics)
}
}
return dialog
}
private fun getHeightPx(displayMetrics: DisplayMetrics): Int {
val heightPixels = displayMetrics.heightPixels
val dimen = 16F.dp(requireContext())
val density = displayMetrics.density
return heightPixels - (dimen * density).toInt()
}
private fun addView() {
val layoutParam = ConstraintLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
)
val view = TextView(context).apply {
textSize = 14F
text = "Added Item"
setTextColor(Color.BLACK)
setPadding(
0,
16F.dp(context),
0,
16F.dp(context),
)
}
binding.container.addView(view, layoutParam)
}
private fun Float.dp(context: Context): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this,
context.resources.displayMetrics
).roundToInt()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.button.setOnClickListener {
addView()
}
}
}Editor is loading...
Leave a Comment