Search code examples
androidanimationfadeinimagebuttonfadeout

What is the best way to implement fade-in and fade-out animations on Android?


I have a few controls that I don't want the user to see all the time, so they need to:

  • Fade out 5 seconds after activity is created
  • Fade in when user taps the screen
  • Fade out after 5 seconds of visibility or when user taps screen again (whichever comes first)

I've seen a lot of implementations of animations out there (including Thread, Handler, and Animation). Which method would work best in this situation?


Solution

  • Use following code for fade in and fade out.

    fade_in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true" >
    
        <alpha
            android:duration="1000"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="1.0" />
    
    </set>
    

    fade_out.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true" >
    
        <alpha
            android:duration="1000"
            android:fromAlpha="1.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="0.0" />
    
    </set>
    

    happy coding !!