0

I disable animation when I set the checked state like this and it's working:

switchView.isChecked = state.isChecked
switchView.jumpDrawablesToCurrentState()

But when I click on SwitchCompat view, the animation works. How can I disable the SwitchCompat animation on click?

shmakova
  • 6,076
  • 3
  • 28
  • 44

1 Answers1

1

There is no exact solution to this problem. You can find the reason here. But there is a little bit of hacky solution that we can use. Take a look at this answer and the Custom SwitchCompatFix.java class.

To make two private methods accessible Java Reflection is used there. Letter, these methods have been used to cancel the animation when triggered. You can use that Custom Switch Class and the code below in order to achieve what you need.

    switchCompatFix = findViewById(R.id.switchCompatFix);
    switchCompatFix.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                if(!switchCompat.isChecked()) {
                    switchCompat.setChecked(true, false);
                }else {
                    switchCompat.setChecked(false, false);
                }
            }
            return true;
        }
    });

By returning true from the onTouch() method, the touch listener will not reach the Parent SwitchComapt class.