i have an custom BttomSheetDialogFragment and i want to have round corners in top of Bottom View
this is my Custom class that inflating my layout that i want to appear from bottom
View mView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.charge_layout, container, false);
initChargeLayoutViews();
return mView;
}
and also i have this xml resource file as background :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners android:topRightRadius="35dp"
android:topLeftRadius="35dp"
/>
<solid android:color="@color/white"/>
<padding android:top="10dp"
android:bottom="10dp"
android:right="16dp"
android:left="16dp"/>
but the problem is, when i set this resource file as background of my Layout's root element , the corners still are not rounded
and i can't use below code :
this.getDialog().getWindow().setBackgroundDrawableResource(R.drawable.charge_layout_background);
cuz its override default background of BottomSheetDialog and there wont be any semi-transparent gray color above my Bottom View
Create a shape drawable .. which we will use as background for bottom sheet . Provide the appropriate value for radius of top left and right corner .
Now create style for " Bottom sheet dialog fragment "
Now create a custom class that will extend BottomSheetDilogFragment ,where you provide your style .
Now use this class wherever you want to have round corner bottom sheet . eg
The
BottomSheetDialog
is setting a default white background color , this is why the corners are not visible, In order to show them you need to make the background of the dialog transparent by overriding the style of theBottomSheetDialog
.Define this style In your
res/values/styles/styles.xml
And set this style to your BottomSheetDialog
Create a custom drawable with rounded corner and set it as background of your BottomSheetDialogFragment's layout root
And then simply add the below code to your BottomSheetDialogFragment class
You can even play with the params to set margin like below
With the new Material Component library you can customize the shape of your component using the
shapeAppearanceOverlay
attribute in your style.Just use the
BottomSheetDialogFragment
overriding theonCreateView
method and then define your custom style for Bottom Sheet Dialogs.Define the
bottomSheetDialogTheme
attribute instyles.xml
in your app theme:Then just define your favorite shape with
shapeAppearanceOverlay
It requires the version 1.1.0 (currently the latest version is
1.1.0-beta02
).If you use the last version of material component you just have to override
ShapeAppearance.MaterialComponents.LargeComponent
(as the bottom sheet use this shape) and set the value you want like :And then set in your app style :
The solution of Gabriele Mariotti is similar and works too but this one is simpler.
I was checking the same thing today and yes you were right about following code
this applies to fragment background, so instead you should get the bottomsheet view from dialog window and change the background here is the code
here bottomsheet is the actual view you want to change.