Android BottomSheetDialogFragment has color behind rounded corners

Viewed 7254

I'm using BottomSheetDialogFragment and I'm rounding the corners of top right/left and it's working properly but I noticed that behind the rounded corners, it's not transparent and it's very annoying.

It's noticeable in the below screenshot:

enter image description here

How do I make them transparent?

3 Answers

Create custom style like below.

 <style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/AppModalStyle</item>
    </style>

    <style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
        <item name="android:background">@drawable/rounded_corner_top_only</item>
    </style>

then override this method in the custom fragment.

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //bottom sheet round corners can be obtained but the while background appears to remove that we need to add this.
        setStyle(DialogFragment.STYLE_NO_FRAME,R.style.AppBottomSheetDialogTheme);
    }

This is working with me hopefully it will work with you.

You have to change the bottom sheet theme to achieve the top round layout

Create a custom drawable background_bottom_sheet_dialog_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <corners
       android:topLeftRadius="8dp"
        android:topRightRadius="8dp" />
    <padding android:top="0dp" />
    <solid android:color="@color/white" />
</shape>

Then override bottomSheetDialogTheme on styles.xml using the drawable as background:

<!--Bottom sheet-->
<style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">
    <item 
    name="android:background">@drawable/background_bottom_sheet_dialog_fragment
    </item>
</style>

<style name="BaseBottomSheetDialog" 
    parent="@style/Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowIsFloating">false</item>
    <item name="bottomSheetStyle">@style/BottomSheet</item>
</style>

<style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog" />

This will change the background layout of your bottom sheet

NOTE: remove all background from your bottom sheet dialog view layout

Override this in your BottomSheetDialogFragment

@Override
public void setupDialog(Dialog dialog, int style) {
    View view = View.inflate(getContext(), R.layout.YOUR_LAYOUT, null);
    dialog.setContentView(view);
    ((View) view.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
Related