I want to create a fully expanded BottomSheetDialogFragment
with top rounded corners, following the technique described in this GitHub comment – https://github.com/material-components/material-components-android/issues/1278#issuecomment-844979762
Before displaying the BottomSheetDialogFragment
, the app extends fully to the bottom of the screen.
Before showing BottomSheetDialogFragment
However, when the dialog is shown in its fully expanded state, the bottom bar does not extend as expected. Please refer to the attached screenshots for a visual reference.
When showing BottomSheetDialogFragment
Here’s our code snippet.
RoundedCornerBottomSheetDialogFragment.kt
class RoundedCornerBottomSheetDialogFragment : BottomSheetDialogFragment() {
private lateinit var binding : RoundedCornerBottomSheetDialogFragmentBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = RoundedCornerBottomSheetDialogFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val bottomSheet : FrameLayout = dialog?.findViewById(com.google.android.material.R.id.design_bottom_sheet)!!
// Height of the view
bottomSheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
// Behavior of the bottom sheet
val behavior = BottomSheetBehavior.from(bottomSheet)
behavior.apply {
state = BottomSheetBehavior.STATE_EXPANDED
}
}
override fun getTheme(): Int {
return R.style.RoundedCornerBottomSheetDialogFragmentStyle
}
}
styles.xml
<resources>
<style name="RoundedCornerBottomSheetDialogFragmentStyle" parent="@style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
<item name="bottomSheetStyle">@style/BottomSheet_NoDrawing</item>
</style>
<style name="BottomSheet_NoDrawing" parent="Widget.MaterialComponents.BottomSheet">
<item name="backgroundTint">@android:color/transparent</item>
</style>
</resources>
rounded_corner_bottom_sheet_dialog_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/rounded_top_corners_background"
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 3" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 4" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 5" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 6" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 7" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 8" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 9" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 10" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 11" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 12" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 13" />
<Button
android:layout_width="wrap_content"
android:layout_height="56dp"
android:text="Hello 14" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
May I know, how I can resolve this issue, to ensure our app remains edge-to-edge?
The complete workable demo can be downloaded from : https://github.com/yccheok/RoundedCornerBottomSheetDialogFragment