ikurek
ikurek

Reputation: 604

Android BottomSheetBehavior setPeekHeight(int peekHeight, boolean animate) does not animate height transition

I'm having a BottomSheetBehavior layout that looks like this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    app:behavior_hideable="false"
    app:behavior_peekHeight="76dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <LinearLayout
        android:id="@+id/bottomSheetPeekLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/bottomSheetEditText"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:layout_margin="16dp"/>

        <include
            android:id="@+id/bottomSheetDynamicHeightLayout"
            layout="@layout/layout_bottom_sheet_dynamic_height"
            android:visibility="gone" />
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/bottomSheetRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

Embedded inside CoordinatorLayout in an Activity:

<LinearLayout
    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"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@color/white">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/closeIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="4dp"
            android:padding="12dp"
            android:src="@drawable/ic_close_point_chooser" />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textAllCaps="true"
            android:textColor="#3f454e"
            android:textSize="14sp" />

    </FrameLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/mapFragmentContainerView"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusableInTouchMode="true" />

        <include
            android:id="@+id/bottomSheetLayout"
            layout="@layout/bottom_sheet_layout"
            android:paddingTop="44dp" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>

Peek height of BottomSheet should be changed, depending on the height of bottomSheetDynamicHeightLayout, which is a constraint layout displaying some data. I observe the height changes onf this layout, and update BottomSheetBehavior this way:

bottomSheetPeekLayout.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            bottomSheetBehavior.setPeekHeight(bottomSheetPeekLayout.height, true)
}

So when my view height changes, BottomSheet peek height is changed using setPeekHeight(int peekHeight, boolean animate) methood, which according to the documentations, should animate height changes.

This however, does not happen - peek height is updated, and bottom sheet changes peek size, but there's no animation - it just changes screen size. When I used this method before, to update height manually (not in a onLayoutChangedListener), the animation worked fine.

Is there anything I'm missing? Is this some sort of bug, or desired behavior? Shouldn't the animate parameter always animate transition? And how can I archieve desired effect?

Upvotes: 2

Views: 5432

Answers (1)

Beeeeeeer
Beeeeeeer

Reputation: 95

I faced a similar problem, and I am not sure of a direct solution using:

setPeekHeight(int peekHeight, boolean animate)

But, you can add a transition using transition manager, as follows:

TransitionManager.beginDelayedTransition(bottomSheet);
bottomSheetBehavior.setPeekHeight(20);

If you want to change the duration of translation, use the following instead:

ChangeBounds transition = new ChangeBounds();
transition.setDuration(200L);
TransitionManager.beginDelayedTransition(bottomSheet, transition);
bottomSheetBehavior.setPeekHeight(20);

Based on the answers from:

  1. animateLayoutChanges="true" in BottomSheetView showing unexpected behaviour

  2. Is it possible to set transition speed/time in transition everywhere on android

Upvotes: 2

Related Questions