The question was asked more than two years ago, but, unfortunately, the problem persists.
I finally got the decision to save the addView and removeView to the BottomSheet, having animateLayoutChanges="true" .
BottomSheetBehavior cannot calculate the correct height when it changes, so the height must remain unchanged. To do this, I set the height of the BottomSheet match_parent and split it into two match_parent : the content and Space which changes the height to match the height of the content.
To best simulate the true behavior of BottomSheet , you also need to add a TouchToDismiss view that darkens the background when expanding the BottomSheet and also close the BottomSheet when the user clicks outside the content.
Here is the code:
activity.xml
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/show_bottom_sheet" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show bottom sheet"/> <View android:id="@+id/touch_to_dismiss" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" android:background="#9000"/> <LinearLayout android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"> <Space android:id="@+id/space" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <LinearLayout android:id="@+id/bottom_sheet_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:animateLayoutChanges="true"> <Button android:id="@+id/add_or_remove_another_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add another view"/> <TextView android:id="@+id/another_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Another view"/> </LinearLayout> </LinearLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
activity.java
BottomSheetBehavior bottomSheetBehavior; View touchToDismiss; LinearLayout bottomSheet; Button showBottomSheet; Space space; LinearLayout bottomSheetContent; Button addOrRemoveAnotherView; TextView anotherView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); touchToDismiss = findViewById(R.id.touch_to_dismiss); touchToDismiss.setVisibility(View.GONE); touchToDismiss.setOnClickListener(this); bottomSheet = findViewById(R.id.bottom_sheet); bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); bottomSheetBehavior.setPeekHeight(0); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { if (newState == BottomSheetBehavior.STATE_HIDDEN || newState == BottomSheetBehavior.STATE_COLLAPSED) { touchToDismiss.setVisibility(View.GONE); }else { touchToDismiss.setVisibility(View.VISIBLE); } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { touchToDismiss.setAlpha(getRealOffset()); } }); showBottomSheet = findViewById(R.id.show_bottom_sheet); showBottomSheet.setOnClickListener(this); space = findViewById(R.id.space); bottomSheetContent = findViewById(R.id.bottom_sheet_content); addOrRemoveAnotherView = findViewById(R.id.add_or_remove_another_view); addOrRemoveAnotherView.setOnClickListener(this); anotherView = findViewById(R.id.another_view); bottomSheetContent.removeView(anotherView); } @Override public void onClick(View v) { if (v == showBottomSheet) bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); else if (v == addOrRemoveAnotherView) { if (anotherView.getParent() == null) bottomSheetContent.addView(anotherView); else bottomSheetContent.removeView(anotherView); } else if (v == touchToDismiss) bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } public float getRealOffset() { float num = (space.getHeight() + bottomSheetContent.getHeight()) - (bottomSheet.getY() + space.getHeight()); float den = bottomSheetContent.getHeight(); return (num / den); }
This is the result obtained with this code: 
Hope this will be helpful to someone, as the problem still exists!
source share