To control or customize the animation of a BottomSheetDialogFragment
, you must set the windowAnimations
attribute at the end of onCreateDialog()
.
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// https://stackoverflow.com/questions/51706883/converting-dialog-to-bottom-sheet
View dialogLayout = LayoutInflater.from(getActivity()).inflate(R.layout.my_dialog_layout, null);
BottomSheetDialog dialog = new BottomSheetDialog(getActivity());
dialog.setContentView(dialogLayout);
// Example of setting a button click listener
Button button1 = dialogLayout.findViewById(R.id.button1);
button1.setOnClickListener(this);
// Control or customize the animation of the BottomSheetDialog here
dialog.getWindow().getAttributes().windowAnimations = R.style.BottomSheetDialogAnimation;
return dialog;
}
There is no easy way to apply a Bézier curve to the animation. The closest you can get is to use "@android:anim/decelerate_interpolator"
or "@android:anim/accelerate_interpolator"
in the animation XML file. These will gradually slow down or speed up the animation near the end.
res/anim/bottom_sheet_slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<!-- Source: https://stackoverflow.com/questions/18232372/slide-a-layout-up-from-bottom-of-screen -->
<translate
android:fromYDelta="75%p"
android:toYDelta="0%p"
android:fillAfter="true"
android:duration="500"/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500"/>
</set>