Devin Dixon
Devin Dixon

Reputation: 12403

Android Set Bottom Sheet State in XML

The Bottom Sheet in XML has several states: STATE_COLLAPSED, STATE_EXPANDED,STATE_HIDDEN, etc.

How can I set the set in the XML using an observable?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:behavior_hideable="true"
    app:behavior_peekHeight="0dp"
    app:layout_behavior="@string/bottom_sheet_behavior"
    <!-- Example of State -->
    app:bottom_state="@{viewModel.state}"
    >
</LinearLayout>

Obviously, bottom state is not correct the name but I am trying to figure out what is.

Upvotes: 2

Views: 3169

Answers (1)

Sudhi
Sudhi

Reputation: 413

No direct xml values are available for bottom sheet states. But if you want to do it in data binding there is another option. Create a binding adapter for that and use that.

@BindingAdapter("bottomSheetState")
fun bindingBottomSheet(container: LinearLayout, state:Int) {
    val behavior=BottomSheetBehavior.from(container)
    behavior.state = state
}

Now in xml you can use it

<LinearLayout
    android:layout_height="340dp"
    android:layout_width="match_parent"
    app:behavior_hideable="true"
    app:behavior_peekHeight="80dp"
    app:bottomSheetState="@{model.uiState.bottomSheetState}"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

Upvotes: 6

Related Questions