Search code examples
androidandroid-recyclerviewandroid-switch

Switch - how to enable default animation in Android


I have a standard unaltered Switch class on every list item in a RecyclerView (in order to toggle an item on or off). Usually switches in Android have a nice default animation with the switch sliding into position and a small bubble animation appears. In my case, the switch just jumps into the final position without any animation. Here is my layout for it:

<Switch
    android:id="@+id/alarm_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:checked="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:animateLayoutChanges="true"
    android:padding="10dp" />

The property android:animateLayoutChanges="true" does not have any effect. Tried on Android M and O. How can I enable the standard animation?


Solution

  • If toggling your Switch executes any java code, it's possible for this to affect whether or not animations play. Perhaps you have something that calls notifyDataSetChanged() (or similar), which will cause an immediate re-draw of the entire RecyclerView. Or perhaps you're accidentally blocking the UI thread, which would prevent animations of any sort.