Col Col - 1 month ago 8
Android Question

Android Bottomsheet TextView height not adjusted on first time expanding

TL;DR:
TextView
in
Bottomsheet
not showing wrapped multi-line text the first time
Bottomsheet
is expanded, but adjust itself after it collapsed.



So I am using the Bottomsheet from
design-23.2.1
library.

My layout file looks like this:

<android.support.design.widget.CoordinatorLayout>

......

<LinearLayout
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:behavior_peekHeight="@dimen/bottom_sheet_peek_height"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"/>
</android.support.design.widget.CoordinatorLayout>


The content of the
Bottomsheet
is basically a list:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false" />
...
</LinearLayout>


The issue is whenever the
Bottomsheet
is set to
STATE_EXPANDED
the first time, the
TextView
is single line and text is wrapped, and there is no ellipsis
at line end.

Then after it's set to
STATE_COLLAPSED
,
TextView
's height is fine and multi-lined properly.

I know height re-layout happened after set to
STATE_COLLAPSED
because I slide it from collapse and the multi-line is already there.

A work around is provided here. I followed it and added this:

bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
bottomSheetBehavior.onLayoutChild(coordinatorLayout,
bottomSheetView,
ViewCompat.LAYOUT_DIRECTION_LTR);
}
}
........
}


It did actually make the height re-adjust when
Bottomsheet
is expanded the first time. However it occurred abruptly right after the expanding animation is done.

Is there any way to adjust the height ahead of the expanding animation just like how Google Map does?

Update

I found that this issue is because I have set
Bottomsheet
to
STATE_COLLAPSED
before it was expanded. If that was not set then the problem is gone and height is adjusted properly the first time.

Now my question is: why set it to
STATE_COLLAPSED
before expanding will cause that issue?

Col Col
Answer

After switching to design library 24.0.0, the issue cannot be reproduced anymore.

Thanks for the efforts from the Android team to make our life easier and easier.

Comments