Introduction

We are living in the era of Smart Phones. Phones are getting smarter day-by-day with their functionality and UI design.
So the animation is an indispensable part of the Android mobile UI, it gives users the look and feel of the model and a good experience as well. Now API level 23 is going on Android and the developer preview version of Android N is out. We can see the differences between the UI part of the pre-Lollipop version, Lollipop, and later versions. A vast design change can be seen in Lollipop and the latest version, like Marshmallow (Android 6.0).

What Animation does in Mobile Applications?

Let's have a look around the code which was needed to make the app look good.
Step 1
Let us take an Android application and select activity using Android Studio version 2.1.
Let's take an Empty Activity,
Step 2
Add a button in xml and take ListView as well,
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.example.gkumar.explorecity.MainActivity">
  11. <TextView
  12. android:id="@+id/basic_text_view"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="Explore your city today"
  16. android:padding="10dp"
  17. android:layout_centerInParent="true"
  18. android:layout_alignParentTop="true"
  19. android:background="@drawable/text_view_background"
  20. android:visibility="visible"/>
  21. <View
  22. android:id="@+id/line"
  23. android:layout_width="match_parent"
  24. android:layout_height="1dp"
  25. android:background="#7f7f7f"
  26. android:layout_below="@+id/basic_text_view"
  27. android:layout_marginTop="20dp"
  28. android:visibility="visible"/>
  29. <ListView
  30. android:id="@+id/categoryListView"
  31. android:layout_width="match_parent"
  32. android:layout_height="match_parent"
  33. android:layout_below="@+id/line"
  34. android:layout_marginTop="15dp"
  35. android:visibility="gone"
  36. >
  37. </ListView>
  38. </RelativeLayout>
    Ok, this will look like below
    Step 3
    Now take another xml for your list part
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:id="@+id/front"
    3. android:layout_width="match_parent"
    4. android:layout_height="wrap_content"
    5. android:background="#ffffff"
    6. android:padding="8dp">
    7. <ImageView
    8. android:id="@+id/leftImageView"
    9. android:layout_width="40dp"
    10. android:layout_height="40dp"
    11. android:layout_alignParentLeft="true"
    12. android:scaleType="fitXY"
    13. android:src="@mipmap/ic_launcher" />
    14. <TextView
    15. android:id="@+id/NameTextView"
    16. android:layout_width="match_parent"
    17. android:layout_height="wrap_content"
    18. android:text="N/A"
    19. android:layout_marginLeft="5dp"
    20. android:textSize="14sp"
    21. android:typeface="sans"
    22. android:layout_centerVertical="true"
    23. android:layout_alignTop="@id/leftImageView"
    24. android:layout_toRightOf="@+id/leftImageView"
    25. />
    26. </RelativeLayout>
    Step 4
    Combining steps 2 and 3 we can actually manage them in MainActivity:
    Let's hover around the code and try to understand it.
    1. package com.example.gkumar.explorecity;
    2. import android.app.Activity;
    3. import android.content.Context;
    4. import android.os.AsyncTask;
    5. import android.support.v7.app.AppCompatActivity;
    6. import android.os.Bundle;
    7. import android.view.LayoutInflater;
    8. import android.view.View;
    9. import android.view.ViewGroup;
    10. import android.view.animation.Animation;
    11. import android.view.animation.AnimationUtils;
    12. import android.widget.BaseAdapter;
    13. import android.widget.ImageView;
    14. import android.widget.ListView;
    15. import android.widget.TextView;
    16. import com.squareup.picasso.Picasso;
    17. import java.util.ArrayList;
    18. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    19. ArrayList<String> completeCategories = new ArrayList<String>();
    20. private TextView basic_text_view;
    21. private ListView mCategoryListView;
    22. private ItemsAdapter mItemAdapter;
    23. @Override
    24. protected void onCreate(Bundle savedInstanceState) {
    25. super.onCreate(savedInstanceState);
    26. setContentView(R.layout.activity_main);
    27. basic_text_view = (TextView) findViewById(R.id.basic_text_view);
    28. mCategoryListView = (ListView) findViewById(R.id.categoryListView);
    29. completeCategories.add(0, "BreakFast/Lunch/Dinner");
    30. completeCategories.add(1, "Movies and Theaters");
    31. completeCategories.add(2, "Shopping");
    32. completeCategories.add(3, "Hotels/Restaurants");
    33. completeCategories.add(4, "Cars/Bikes");
    34. mItemAdapter = new ItemsAdapter(this, completeCategories);
    35. mCategoryListView.setAdapter(mItemAdapter);
    36. basic_text_view.setOnClickListener(this);
    37. }
    38. @Override
    39. public void onClick(View v) {
    40. switch (v.getId()) {
    41. case R.id.basic_text_view:
    42. mCategoryListView.setVisibility(View.VISIBLE);
    43. break;
    44. }
    45. }
    46. // Adapter class
    47. public class ItemsAdapter extends BaseAdapter {
    48. ArrayList<String> list;
    49. Context context;
    50. int commentId;
    51. public ItemsAdapter(Activity activity,
    52. ArrayList<String> mDataList) {
    53. super();
    54. list = mDataList;
    55. context = activity;
    56. }
    57. @Override
    58. public int getCount() {
    59. // TODO Auto-generated method stub
    60. return list.size();
    61. }
    62. @Override
    63. public Object getItem(int position) {
    64. // TODO Auto-generated method stub
    65. return list.get(position);
    66. }
    67. @Override
    68. public long getItemId(int position) {
    69. // TODO Auto-generated method stub
    70. return position;
    71. }
    72. @Override
    73. public View getView(int position, View convertView, ViewGroup parent) {
    74. final ViewHolder holder;
    75. int finalPosition = -1;
    76. if (convertView == null) {
    77. LayoutInflater vi = (LayoutInflater) context
    78. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    79. convertView = vi.inflate(R.layout.category_list_row_data_items,
    80. parent, false);
    81. holder = new ViewHolder();
    82. holder.commentNameTextView = (TextView) convertView
    83. .findViewById(R.id.NameTextView);
    84. holder.categoryImage = (ImageView) convertView.findViewById(R.id.leftImageView);
    85. convertView.setTag(holder);
    86. } else {
    87. holder = (ViewHolder) convertView.getTag();
    88. }
    89. holder.commentNameTextView.setText(list.get(position));
    90. /** we can use picaso as well but no need this time**/
    91. // Picasso.with(getApplicationContext()).load(R.drawable.superman).into(holder.categoryImage);
    92. Animation animation = AnimationUtils.loadAnimation(MainActivity.this, (position > finalPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
    93. convertView.startAnimation(animation);
    94. finalPosition = position;
    95. return convertView;
    96. }
    97. }
    98. public class ViewHolder {
    99. TextView commentNameTextView;
    100. ImageView categoryImage;
    101. }
    102. }
      Now, let's see the method onCreate(); all the views have been found there or referenced from the XML part, and the same is happening in the Adapters class method getView(), using view holder pattern as new class ViewHolder you can see it in the code.
      Step 5
      On the click of the button that we have already created, we will show our list using animation.
      Now the code for animation is below
      Firstly, put these two files in the animation directory called “anim”. You are wondering what are these two files and what do they do. So the answer would look like the following
      Down_from_top.xml
      1. <?xml version="1.0" encoding="utf-8"?>
      2. <set xmlns:android="http://schemas.android.com/apk/res/android"
      3. android:shareInterpolator="@android:anim/decelerate_interpolator">
      4. <translate
      5. android:fromXDelta="0%" android:toXDelta="0%"
      6. android:fromYDelta="-100%" android:toYDelta="100%"
      7. android:duration="500" />
      8. </set>
        Up_from_bottom.xml
        1. <?xml version="1.0" encoding="utf-8"?>
        2. <set xmlns:android="http://schemas.android.com/apk/res/android"
        3. android:shareInterpolator="@android:anim/decelerate_interpolator">
        4. <translate
        5. android:fromXDelta="0%" android:toXDelta="0%"
        6. android:fromYDelta="100%" android:toYDelta="0%"
        7. android:duration="400" />
        8. </set>
          Result after animation
          Note
          In this pic, you can't see the animation; please run this code as the entire project is provided and you will see the animation from down to up translates.

          Summary

          This article explains the basics of animation in Android. Basically, it is similar to Google plus animation and animations are very much in demand in the mobile application world.
          Read more articles on Android