24

I'm curious as to this behavior ... I'm currently setting the two values in the anim XML:

    android:fillEnabled="true"
    android:fillAfter="true"

However, the transformation does not apply after the animation is done ... it always resets. When I set it programmatically via code it does seem to work:

    animation.setFillEnabled(true);
    animation.setFillAfter(true);

So I'm just curious how this should work, as I'd rather set it on the XML. Thanks!

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185

4 Answers4

69

I had the same problem, this worked for me:

<set
    android:fillEnabled="true"
    android:fillAfter="true"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:fromYDelta="0"
        android:toYDelta="-20%p"
        android:duration="7000" />

</set>

Put the attributes fillEnabled and fillAfter in the Set tag.

Josh Cole
  • 1,522
  • 2
  • 12
  • 19
  • 1
    I was having this issue with a RotateAnimation and your comment helped solve the issue. Cheers. – denizmveli May 09 '12 at 01:26
  • thanks a lot, did the trick for me :) link you to another post :) – cV2 Aug 13 '12 at 13:39
  • 1
    Doesnt work for my animations with min and target set 14 and 19 respectively on a Nexus 4 with 4.4.4. Animation always reverts back to original ImageView. – RichieHH Jul 20 '14 at 20:55
  • Hi @RichieHH I am facing the same problem. Did you get any solution to your problem ? – Somir Saikia Dec 05 '14 at 12:56
  • as xml sequential loading animation and starting cause this problem with one animation with fillafter true works perfect – Ucdemir Aug 28 '18 at 12:52
14

It also works if you don't have the set tag and are just doing translate or something like so.

<translate xmlns:android="http://schemas.android.com/apk/res/android"       
    android:interpolator="@android:anim/linear_interpolator"       
    android:fromXDelta="0"
    android:toXDelta="0"
    android:fromYDelta="1000"
    android:toYDelta="-300" 
    android:startOffset="100"
    android:duration="1000"
    android:fillAfter="true" />
aldok
  • 17,295
  • 5
  • 53
  • 64
userman
  • 301
  • 2
  • 4
8

In general, use the fillAfter and fillEnabled on the root element

so either

<?xml version="1.0" encoding="utf-8"?>
<translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true"
    android:fromXDelta="0%"
    android:toXDelta="-100%"
    android:duration="500" />

OR

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true">
    <translate
        android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:duration="500" />
</set>
SoliQuiD
  • 2,093
  • 1
  • 25
  • 29
0

putting the fillEnabled and fillAfter attributes in the set tag helped solved the issue.

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
Ramesh
  • 33
  • 4