Persistent BottomSheet peekHeight and STATE_COLLAPSED are not drawn correctly at the beginning of the action

I have a CoordinatorLayout with a custom view and a constant bottom sheet. I turned off hiding the bottom sheet and want to show the top pair of views in LinearLayout when folded. To account for different screen sizes, I dynamically set peekHeight in my onCreate activity, after everything has been laid out.

The problem is that the activity starts first, the BottomSheet looks at one height, and then, after it has expanded and collapsed again, peekHeight seems to have changed. Interestingly, it visually looks like this: adding peehHeight is filling in layout activity (16dp). However, the BottomSheetBehavior reports the same peekHeight for both.

I created a hacky workaround for this, but I want to see if there is something that I am doing wrong. Below is a gif showing when I don't have my workaround, and one when I use my workaround. I also included the appropriate code and layout files. Again, this seems to be directly related to my add-on (16dp), but I don’t understand how to fix it.

DO NOT use workaround:

enter image description here

Using a workaround:

enter image description here

Activity:

@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.edit_schedule_details_activity); final CoordinatorLayout activityLayout = (CoordinatorLayout) findViewById(R.id.edit_schedule_details_layout); ButterKnife.bind(this); // ... removed unrelated logic final View bottomSheet = findViewById(R.id.edit_details_sheet); final LinearLayout peekContainer = (LinearLayout) findViewById(R.id.info_title_container); activityLayout .getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // Set the measured peek height and add in 16 dps to account for inconsistency // Adding the 16 dps is part of the workaround bottomSheetBehavior.setPeekHeight(peekContainer.getHeight()); //+ (int) (16 * getResources().getDisplayMetrics().density)); /* workaround */ Timber.i("Title height: " + bottomSheetBehavior.getPeekHeight()); bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); } }); bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(View bottomSheet, int newState) { String stateString = ""; if (newState == BottomSheetBehavior.STATE_EXPANDED) { stateString = "Expanded"; /* workaround */ /*if(!bottomSheetSet){ // remove the extra 16 dp added since the layout now looks correct bottomSheetBehavior .setPeekHeight( bottomSheetBehavior.getPeekHeight() - (int) (16 * getResources().getDisplayMetrics().density)); bottomSheetSet = true; }*/ } else if (newState == BottomSheetBehavior.STATE_COLLAPSED) { stateString = "Collapsed"; Timber.i("peekHeight = " + bottomSheetBehavior.getPeekHeight()); } else if (newState == BottomSheetBehavior.STATE_HIDDEN) { stateString = "Hidden"; } else if(newState == BottomSheetBehavior.STATE_SETTLING) { stateString = "Settling"; } else if(newState == BottomSheetBehavior.STATE_DRAGGING){ stateString = "Dragging"; } Timber.i("bottomSheetState = " + stateString); } @Override public void onSlide(View bottomSheet, float slideOffset) { } }); } 

Markup:

 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/edit_schedule_details_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/activity_horizontal_margin" android:background="#ff171e26"> <com.cel.cortetcommercial.widget.ring_view.RingView android:id="@+id/edit_ringview" android:layout_width="match_parent" android:layout_height="400dip" app:inner_radius="95dip" app:outer_radius="130dip" app:color="@color/colorSecondaryDark" app:is_touchable="true" app:text_size="12dip" app:tick_length="0dip" app:tick_width="0dip" android:transitionName="@string/transition_details_to_edit"/> <LinearLayout android:id="@+id/add_remove_button_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" app:layout_anchor="@id/edit_ringview" app:layout_anchorGravity="bottom"> <Button android:id="@+id/remove_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:layout_gravity="end"/> <Button android:id="@+id/add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+"/> </LinearLayout> <android.support.v4.widget.NestedScrollView android:id="@+id/edit_details_sheet" android:layout_width="match_parent" android:layout_height="250dp" android:clipToPadding="true" android:background="@color/colorPrimary" app:layout_behavior="android.support.design.widget.BottomSheetBehavior" app:behavior_hideable="false"> <include layout="@layout/current_info_sheet"> </include> </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> 

current_info_sheet:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/info_container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/info_title_container"> <TextView android:id="@+id/segment_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:text="@string/placeholder" android:textAppearance="@android:style/TextAppearance.Material.Headline" android:textColor="@color/white"/> <TextView android:id="@+id/time_remaining" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_marginBottom="@dimen/activity_vertical_margin" android:text="@string/placeholder" android:textColor="@color/white"/> <Space android:layout_width="1dp" android:layout_height="16dp"/> </LinearLayout> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:textColor="@color/white" android:text="@string/luminosity"/> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin"/> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:textColor="@color/white" android:text="@string/color_warmth"/> <SeekBar android:id="@+id/seekBar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="@dimen/activity_vertical_margin" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginRight="@dimen/activity_horizontal_margin"/> </LinearLayout> 
+7
android android-layout android-coordinatorlayout bottom-sheet
source share

No one has answered this question yet.

See related questions:

2735
Stop EditText from getting focus when starting Activity
2510
How to keep Android activity state by saving instance state?
1296
Restart activity during Android rotation
1270
How to transfer data between actions in an Android application?
1042
As a result, a window leaked that was originally added
935
Cannot start Eclipse - Java was started, but returned exit code = 13
927
You must use Theme.AppCompat (or child) theme with this action
837
How to hide the title bar for an Activity in XML with an existing custom theme
586
How to start a new activity at the click of a button
6
How to play audio file in Android?

All Articles