How to setup animation in android

Date : 20/06/2019


Animation in android application are used to remove raw motions in android. However without an animation there can’t be good user experience so, we used. As a simple you can write an animation in XML file and use in your layout XML file.

An animation resource can define one of two types of animations:

Property Animation

Then creates an animation by modifying an object’s property values over a set period of time with an Animator.

View Animation
There is two types in this framework.

Tween animation : Creates animation by performing series of transformation in a single image with animation.

Frame animation : Creates animation by showing sequence of images with animationdrawable.

Here i will only explain briefly about view animator. Then required elements in view animator are explain a little.



Container that holds animation elements. You can specify <set> inside a <set> as nested.


A fade-in or fade-out animation. 

android:fromAlpha. Starting opacity offset, where 0.0 is transparent and 1.0 is opaque

android:toAlpha. Ending opacity offset, where 0.0 is transparent and 1.0 is opaque.


Performs an animation over a specified amount of time.


A vertical and/or horizontal motion.

Supports the attributes are in this three formats:

values from -100 to 100 ending with “%”, indicating a percentage relative to itself.

values from -100 to 100 ending in “%p”, indicating a percentage relative to its parent; a float value with no suffix, indicating an absolute value.


Step 1 : Here is a simple animation XML file which animate from right to left in view for the period mentioned in duration filed.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android=""

        android:toXDelta="0" />

        android:toAlpha="1" />


Step 2 : Basically you need to create XML file with layoutAnimation container in your another XML file.

file name with layout_animation_right_to_left.xml is :

<?xml version="1.0" encoding="utf-8"?>
    xmlns:android=""    android:layout_width="match_parent"


Step 3 : After that, the implementation of animation in your java should be like the following

final LayoutAnimationController controller =
        AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_right_to_left);

With the LayoutAnimationController object, you have set it to whatever view you want to animate.

Thank you for using pheonixsolutions.

If you find it useful, share it with your friends to keep it alive.

Leave a Reply