0

Situation: I have a BottomSheet that have a button (ImageView) in the upper-right corner to close it manually, when ever I click that button the app crashes (below is the code and the LogCat)

PS: I am working with fragments

JAVA Code

bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            // React to state change
            if (newState == BottomSheetBehavior.STATE_EXPANDED){
                    CloseSheet.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if(event.getAction() == MotionEvent.ACTION_DOWN){
                            }
                            else if(event.getAction() == MotionEvent.ACTION_UP){
                                  //This is wher the error happens
                                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);                                    
                            }
                            else if(event.getAction() == MotionEvent.ACTION_CANCEL){
                            }
                            return true;
                        }
                    });
            }else if (newState == BottomSheetBehavior.STATE_DRAGGING){
               bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
            }
        }

LogCat

FATAL EXCEPTION: main
Process: com.incorp.labs.appdict, PID: 26159
java.lang.IllegalArgumentException: Illegal state argument: 5
   at android.support.design.widget.BottomSheetBehavior.startSettlingAnimation(BottomSheetBehavior.java:624)
   at android.support.design.widget.BottomSheetBehavior.setState(BottomSheetBehavior.java:548)
Thorvald
  • 3,424
  • 6
  • 40
  • 66
  • First glance, you always return true, shouldnt it only return true if you do something? – Tacolibre Dec 16 '16 at 22:48
  • I've been always returning true every time I use `onTouchListener` and nothing happend – Thorvald Dec 16 '16 at 22:50
  • Are you sure you don't want `STATE_COLLAPSED` instead of `STATE_HIDDEN`? Looking at the source code, the Behavior is going to be _not_ hideable by default, and if it's not hideable, calling `setState(STATE_HIDDEN)` will throw that Exception. Also, why don't you just use an `OnClickListener`? – Mike M. Dec 16 '16 at 23:45
  • yes, I want it to be hideable – Thorvald Dec 16 '16 at 23:48
  • Then you need to explicitly set it to hideable, since, as I mentioned, it is not, by default. – Mike M. Dec 16 '16 at 23:59
  • @MikeM.can you help me further ? how exactly should I do that ? (provide code if possible) – Thorvald Dec 17 '16 at 00:00
  • 1
    There's the [`behavior_hideable` XML attribute](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.html#attr_android.support.design:behavior_hideable), or the [`setHideable(boolean)` method](https://developer.android.com/reference/android/support/design/widget/BottomSheetBehavior.html#setHideable(boolean)) available in your Java code. Use one or the other with `true`. – Mike M. Dec 17 '16 at 00:04
  • @MikeM. finally ! that worked fine, one more thing please, how to disable the dragging (the event when the user drag the bottomsheet to close it) ? I tried `behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { if (newState == BottomSheetBehavior.STATE_DRAGGING) { behavior.setState(BottomSheetBehavior.STATE_EXPANDED); } }` but it didn't work – Thorvald Dec 17 '16 at 00:13
  • Dunno. I've not really played with `BottomSheetBehavior` much, but the solution in [this post](http://stackoverflow.com/a/36775907) seems like it would be more reliable, and less hacky, than the `setState()` workaround. – Mike M. Dec 17 '16 at 00:19

0 Answers0