Android Animation Example

Android Animation is used to give the UI a rich look and feel. Animations in android apps can be performed through XML or android code. In this android animation tutorial we’ll go with XML codes for adding animations into our application.

Android Animation

Animation in android apps is the process of creating motion and shape change. The basic ways of animation that we’ll look upon in this tutorial are:

  1. Fade In Animation
  2. Fade Out Animation
  3. Cross Fading Animation
  4. Blink Animation
  5. Zoom In Animation
  6. Zoom Out Animation
  7. Rotate Animation
  8. Move Animation
  9. Slide Up Animation
  10. Slide Down Animation
  11. Bounce Animation
  12. Sequential Animation
  13. Together Animation

Android Animation Example XML

We create a resource directory under the res folder names anim to keep all the xml files containing the animation logic. Following is a sample xml file showing an android animation code logic.


Loading Animation when UI widget is clicked

Our aim is to show an animation when any widget(lets say TextView) is clicked. For that we need to use the Animation Class. The xml file that contains the animation logic is loaded using AnimationUtils class by calling the loadAnimation() function. The below snippet shows this implementation.

To start the animation we need to call the startAnimation() function on the UI element as shown in the snippet below:

Here we perform the animation on a textview component by passing the type of Animation as the parameter.

Setting the Animation Listeners

This is only needed if we wish to listen to events like start, end or repeat. For this the activity must implement AnimationListener and the following methods need to overridden.

  • onAnimationStart : This will be triggered once the animation started
  • onAnimationEnd : This will be triggered once the animation is over
  • onAnimationRepeat : This will be triggered if the animation repeats

Android Animation Project Structure


As you can see, we’ve included the xml of all the major types of animations covered above.

Android Animation Examples XML Code

Here I am providing sample code for most of the common android animations.

Fade In Animation


Here alpha references the opacity of an object. An object with lower alpha values is more transparent, while an object with higher alpha values is less transparent, more opaque. Fade in animation is nothing but increasing alpha value from 0 to 1.

Fade Out Animation


Fade out android animation is exactly opposite to fade in, where we need to decrease the alpha value from 1 to 0.

Cross Fading Animation

Cross fading is performing fade in animation on one TextView while other TextView is fading out. This can be done by using fade_in.xml and fade_out.xml on the two TextViews. The code will be discussed in the

Blink Animation


Here fade in and fade out are performed infinitely in reverse mode each time.

Zoom In Animation


We use pivotX="50%" and pivotY="50%" to perform zoom from the center of the element.

Zoom Out Animation


Notice that android:from and android:to are opposite in zoom_in.xml and zoom_out.xml.

Rotate Animation


A from/toDegrees tag is used here to specify the degrees and a cyclic interpolator is used.

Move Animation


Slide Up Animation


It’s achieved by setting android:fromYScale=”1.0″ and android:toYScale=”0.0″ inside the scale tag.

Slide Down Animation


This is just the opposite of slide_up.xml.

Bounce Animation


Here bounce interpolator is used to complete the animation in bouncing fashion.

Sequential Animation


Here a different android:startOffset is used from the transitions to keep them sequential.

Together Animation


Here android:startOffset is removed to let them happen simultaneously.


The activity_main.xml layout consists of a ScrollView and RelativeLayout (we’ll discuss this in a later tutorial) in which every animation type is invoked on the text using their respective buttons. The xml file is shown below :


To sum up, a RelativeLayout, as the name suggests the arrangement of UI Components is relative to each other.

The file contains the onClick Listeners for every button related to its animation type. It’s source code is given below.

As discussed before each textView animation is started by invoking the respective animation object in which the animation logic is loaded by AnimationUtils.loadAnimation() method. The crossFade animation consists of two TextViews in which one fades out and the other fades in.

Below is a short video showing all the animations in our application.
android animation example

The together animation is seen in the image above. Note that these animation when run on an emulator wont be smooth, so it’s recommended to run the application on a normal device.

This brings an end to android animation example tutorial. You can download the Android Animation Example Project from the link below.

By admin

Leave a Reply

%d bloggers like this: