3

Recently,I am using the widget named switch. The switch is default widget of Android Studio.The problem is that when I add new Item with switch,its animation will run but I don't want to see it. I used ListView and Cursor Adapter in my project.And the switch is a item as an element of ListView. Do you know how to solve it?

Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93
Little pupil
  • 31
  • 1
  • 4
  • 2
    Some code block would help us to answer. – Rohit5k2 Feb 24 '16 at 13:29
  • I use two activities and the first activity has a switch widget.And I expose the attribute named checked which is used to change switch's state.I use startIntent to switch two activities.Fortunately,My problem is solved. – Little pupil Feb 28 '16 at 05:53

4 Answers4

9

Calling jumpDrawablesToCurrentState() will disable the animation, if you call it immediately after calling setChecked() on the switch.

comp500
  • 354
  • 4
  • 14
0

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):

@Override
public void setChecked(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.

Simon
  • 2,643
  • 3
  • 40
  • 61
Alexandr Shutko
  • 1,857
  • 2
  • 20
  • 27
  • I have read the source code of the switch but I still have no idea how to solve this problem.Therefore,I raised the problem in the website.But I have found that when I added a new item to databse,there is no animation. Thank you anyway! – Little pupil Feb 26 '16 at 07:04
  • It is simple! Just copy Switch file content to your project as MySwitch and replace animateThumbToCheckedState(checked); to setThumbPosition(checked ? 1 : 0); Also you can toggle switch while it invisible. – Alexandr Shutko Feb 26 '16 at 07:10
  • @AlexandrShutko have you tried it yourself? When copying the source code of the Switch widget to the project you'll get a lot of compilation errors since there will be many unresolvable classes inside the source code. – Roman Samoilenko Sep 19 '18 at 06:55
  • Yes. I tried it. It was almost two years ago I, so I don't remember details, but there was no such problems. – Alexandr Shutko Sep 20 '18 at 07:11
0

If you're having unwanted animation, when Switch is displayed for the 1st time, then it may help you to use SwitchCompat instead.

Peter
  • 340
  • 4
  • 13
-1

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.

Johnny C
  • 1,799
  • 1
  • 16
  • 27