Android : How To Disable The Animation Of Switch?
Solution 1:
Calling jumpDrawablesToCurrentState() will disable the animation, if you call it immediately after calling setChecked() on the switch.
Solution 2:
This is a piece of code from Switch widget (you can see it too - just press Ctrl and click Switch in your Android Studio project window):
@OverridepublicvoidsetChecked(boolean checked) {
super.setChecked(checked);
// Calling the super method may result in setChecked() getting called// recursively with a different value, so load the REAL value...
checked = isChecked();
if (getWindowToken() != null && ViewCompat.isLaidOut(this) && isShown()) {
animateThumbToCheckedState(checked);
} else {
// Immediately move the thumb to the new position.cancelPositionAnimator();
setThumbPosition(checked ? 1 : 0);
}
}
It will always show animation when visible. So you can't disable it.
But you can extend CompoundButton class and make your own switch widget without any animation.
Also you can copy code from Switch widget, rename it, remove animation, make some drawables and you'll get a non-animated switch.
Solution 3:
If you're having unwanted animation, when Switch is displayed for the 1st time, then it may help you to use SwitchCompat instead.
Solution 4:
I was able to disable the animation of the switch by removing it from the view hierarchy, calling setChecked(val) while it's removed, and then readding it to the view hierarchy.
It's kludgey, but as far as I can tell it's the only way to do it -- you can't subclass Switch to override the setChecked() behavior because you still need to call super.setChecked() which will run Switch#setChecked() and do the animation anyway.
Post a Comment for "Android : How To Disable The Animation Of Switch?"