use any constraint custom layout which you want and pass layout behaviour
app:layout_behavior="app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior""
use in class file
use global variable
private var mBottomSheetBehavior: BottomSheetBehavior<*>? = null
In on view created
mBottomSheetBehavior = BottomSheetBehavior.from(view);
mBottomSheetBehavior?.peekHeight = 0
setBottomSheetAndCallBackBottomSheetBehaviour();
bottomSheetCollapsed();
bottomSheet?.visibility = View.VISIBLE
and when create your view called method and pass your layout id and peek height is used for hide view first time.
/**
* set bottom sheet behavior and state
*/
private fun setBottomSheetAndCallBackBottomSheetBehaviour() {
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_HIDDEN
//callback
mBottomSheetBehavior?.setBottomSheetCallback(object :
BottomSheetBehavior.BottomSheetCallback() {
override fun onStateChanged(bottomSheet: View, newState: Int) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetCollapsed()
}
}
override fun onSlide(bottomSheet: View, slideOffset: Float) {}
})
}
and Use following method for expend and collapse.
private fun bottomSheetExpand() {
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
}
private fun bottomSheetCollapsed() {
mBottomSheetBehavior?.state = BottomSheetBehavior.STATE_COLLAPSED
}
and on click of view use
fun isExpendCollapse(){
if (mBottomSheetBehavior?.state == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetExpand()
} else {
bottomSheetCollapsed()
}
}
check xml file CoordinatorLayout is must for bottomsheet behaviour
<android.support.design.widget.CoordinatorLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:clipToPadding="true"
android:visibility="gone"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
android:layout_alignParentBottom="true"
>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorAccent"
app:layout_behavior="app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
/>
</android.support.design.widget.CoordinatorLayout>
You can constraint layout ,linear or any view instead of View. and ihave set cordinate layout with relative layout(parent layout) , you can use as per your requirement.
use any constraint custom layout which you want and pass layout behaviour
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
use in class file
use global variable
private BottomSheetBehavior mBottomSheetBehavior = null;
In on create
layout=(CoordinatorLayout)findViewById(R.id.bottomSheet);
views=(View)findViewById(R.id.view);
mBottomSheetBehavior = BottomSheetBehavior.from(views);
mBottomSheetBehavior.setPeekHeight(0);
setBottomSheetAndCallBackBottomSheetBehaviour();
bottomSheetCollapsed();
layout.setVisibility(View.VISIBLE);
and when create your view called method and pass your layout id and peek height is used for hide view first time.
private void setBottomSheetAndCallBackBottomSheetBehaviour() {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
bottomSheetCollapsed();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});
}
and Use following method for expend and collapse.
private void bottomSheetExpand() {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
private void bottomSheetCollapsed() {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
and on click of view use
void isExpendCollapse(){
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_COLLAPSED){
bottomSheetExpand();
}
else {
bottomSheetCollapsed();
}
}
check xml file CoordinatorLayout is must for bottomsheet behaviour
<android.support.design.widget.CoordinatorLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:clipToPadding="true"
android:visibility="gone"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
android:layout_alignParentBottom="true"
>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/colorAccent"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
/>
</android.support.design.widget.CoordinatorLayout>
You can constraint layout ,linear or any view instead of View. and ihave set cordinate layout with relative layout(parent layout) , you can use as per your requirement.