Android - Simple Animation

Submitted by: 
Operating System: 
Visitors have accessed this post 1735 times.

In this tutorial we will try to create a Simple Animation using Android. This application can be applied to any existing android application to make a stunning design. Android is a mobile operating system developed by Google. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provide an adaptive framework that allow the developer to develop an apps in a simpler way. So let's now do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href=""
  3. " rel="nofollow">"
  4. </a> xmlns:app="<a href=""
  5. " rel="nofollow">"
  6. </a> xmlns:tools="<a href=""
  7. " rel="nofollow">"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simpleanimation.MainActivity">
  12. <TextView
  13. android:layout_height="wrap_content"
  14. android:layout_width="wrap_content"
  15. android:layout_centerHorizontal="true"
  16. android:text="Simple Animations"
  17. android:textSize="29sp"
  18. android:id="@+id/tv_title"
  19. android:layout_marginTop="20dp" />
  21. <TextView
  22. android:layout_height="wrap_content"
  23. android:layout_width="wrap_content"
  24. android:id="@+id/tv_display"
  25. android:layout_centerHorizontal="true"
  26. android:layout_marginTop="50dp"
  27. android:layout_below="@+id/tv_title"
  28. android:text="Hello World"
  29. android:textSize="30sp" />
  31. android:layout_height="wrap_content"
  32. android:layout_width="wrap_content"
  33. android:layout_below="@+id/tv_display"
  34. android:id="@+id/btn_animate"
  35. android:layout_marginTop="50dp"
  36. android:text="Animate"
  37. android:layout_centerHorizontal="true" />
  41. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href=""
  3. " rel="nofollow">"
  4. </a> package="com.razormist.simpleanimation">
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

Creating Animation

This contains the code for the animation. This will animate the TextView when the button is clicked. To do that right create a new directory inside the res directory called anim. Then inside the newly created directory create a resource file called animation and write these code as shown below.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="<a href=""
  3. " rel="nofollow">"
  4. </a> android:fillAfter="true">
  5. <translate
  6. android:duration="1000"
  7. android:fromXDelta="-100%"
  8. android:toXDelta="80%"
  9. android:repeatMode="reverse"
  10. android:repeatCount="infinite">
  11. </translate>
  12. <rotate
  13. android:fromDegrees="0"
  14. android:toDegrees="360"
  15. android:pivotX="50%"
  16. android:pivotY="50%"
  17. android:repeatMode="reverse"
  18. android:repeatCount="infinite"
  19. android:duration="1000"/>
  20. </set>

The Main Function

This code contains the main function of the application. This code will trigger the animation method when the button is clicked. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.

  1. Button btn_animate;
  2. TextView tv_display;
  3. Animation animation;

Finally, initialize the require methods inside the onCreate method to run the application.

  1. btn_animate = (Button)findViewById(;
  2. tv_display = (TextView)findViewById(;
  3. animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation);
  5. btn_animate.setOnClickListener(new View.OnClickListener() {
  6. @Override
  7. public void onClick(View v) {
  8. tv_display.startAnimation(animation);
  9. }
  10. });

Try to run the app and see if it worked.

There you have it we have created a Simple Animation using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.