2 years ago

#53306

test-img

javadroid

Android material bottomsheet half-expanded state with dynamic height

I crateed a bottomSheet with this xml code:

    <?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C57C3C"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvState"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="initial state" />

    <LinearLayout
        android:id="@+id/linBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <include
            layout="@layout/dialog_bottomsheet_half"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

on my bottom sheet ui i have three linearlayout. named linHeader , linOne and linTwo. in first state this linearlayouts is gone except linHeader.

i use this code for showing my bottom sheet:

val  behavior = BottomSheetBehavior.from(linBottomSheet)
 behavior.isFitToContents = false
  behavior.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {
                when (newState) {

                    BottomSheetBehavior.STATE_COLLAPSED -> {
                        Log.e("bottom", "STATE_COLLAPSED")
                    }

                    BottomSheetBehavior.STATE_HALF_EXPANDED -> {
                        Log.e("bottom", "STATE_HALF_EXPANDED")
                    }

                    BottomSheetBehavior.STATE_EXPANDED -> {
                            Log.e("bottom", "STATE_EXPANDED")
                    }
                    BottomSheetBehavior.STATE_DRAGGING -> {
                        Log.e("bottom", "STATE_DRAGGING")
                    }
                    BottomSheetBehavior.STATE_HIDDEN -> {
                        Log.e("bottom", "STATE_HIDDEN")
                    }
                    BottomSheetBehavior.STATE_SETTLING -> {
                        Log.e("bottom", "STATE_SETTLING")
                    }
                }

            }

            var oldOffSet = 0f
            override fun onSlide(bottomSheet: View, slideOffset: Float) {
                val inRangeExpanding = oldOffSet < slideOffset
                val inRangeCollapsing = oldOffSet > slideOffset
                oldOffSet = slideOffset
            }
        })

for enabling half-expand state i need to use this line:

  behavior.isFitToContents = false

and i need to make linOne visible when bottomSheet is on half-expanded state. and when i drag layout to expanded ui, the linTwo is also visible at the moment. but because of using

 behavior.isFitToContents = false

in half-expanded state the bottomSheet height is not fit to content.

how can i solve this issue?

android

xml

kotlin

bottom-sheet

0 Answers

Your Answer

Accepted video resources