'Disable use of background view from the bottomsheet

How to disable background view from the bottomsheet currently it is accessible from the bottomsheet. I have created transparant bottomsheet when I open it It also access the background view. How to disable to access the background view to access from the bottomsheet.My background view is recyclerview and currently it can be accessible from the bottomsheet.



Solution 1:[1]

Override the dispatchtouchEvent method in the activity that includes the Bottom Sheet. Then when you get a touch event, check to see whether that bottom sheet is visible or not and whether that click happened inside it. If that’s the case absorb it so it doesn’t fall through and affect your Recycler view. You can do that by making the event return true to show that you’ve handled it yourself. Here’s a sample code:

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (bottomSheetBehavior.getState() != BottomSheetBehavior.STATE_HIDDEN) {
            Rect outline = new Rect();
            bottomSheetLayout.getGlobalVisibleRect(outline);
            if (!outline.contains((int) event.getRawX(), (int) event.getRawY())) {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                return true;
            }
        }
    }
    return super.dispatchTouchEvent(event);
}

The above code listens for touch events; when it receives one it checks whether the bottom sheet is showing and it draws a rectangle around it (invisible to the user) to assess whether that touch event falls within the bottom sheet. If it's not within it, it makes the entire Bottom Sheet disappear and absorbs the event so that the recycler view below doesn't receive it.

Solution 2:[2]

I had the same problem as you. My solution was to hide my parents layout. Just hide the background when the bottom Sheet is collapsed and set its visibility when the bottom sheet is hidden.

    <RelativeLayout 
    android:Id="@/rel"
    android:layout_with="match_parent"
    android:layout_height="match_parent
android:visibility="visible"/>
    
    <TextView ......./>
    </RelativeLayout>

You retrieve the state of bottom sheet:

case BottomSheetBehavior.STATE_COLLAPSED:
.......
break;
case BottomSheetBehavior.STATE_HIDDEN:
rel.setVisibity(View.VISIBLE);
break;
case BottomSheetBehavior.STATE_EXPANDED:
rel.setVisibity(View.INVISIBLE);
break;

If it is a button that you use to expand and collapse:

Button.setOnClickListener{ ....... 
  BottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); rel.setVisibity(View.INVISIBLE)
} 
Button.setOnClickListener{ .......  
  BottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); rel.setVisibity(View.INVISIBLE)
}

Hope it will help and I'm not late ;)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ouflak