How To Create Animation Using ViewAnimator In Xamarin With Visual Studio 2015

Introduction

In Android application development, sometimes we need to use framing or sliding type effects or an animation when the user clicks on “Next” button or “Previous” button. This can be done by “ViewAnimator” component. In this article, I will show how we can use ViewAnimator in Xamarin with Visual Studio 2015. ViewAnimator is mainly used to animate the views on the screen. It switches smoothly between two or more views and thus provides a way of transitioning from one view to another through the appropriate animations.

The steps are given below to create ViewAnimator.

Step 1

Create new project for an Android Application
 
 

I have selected “Blank App(Android)” template for this article.

Step 2

Application layout

I have used one Android layout to create ViewAnimator. It is “main.axml” layout, which contents ViewAnimator and two buttons are “Next” and “Previous”. AXML code of layout is shown below.

Main.axml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#fff"  
  6.     android:orientation="vertical">  
  7.   
  8.     <ViewAnimator  
  9.         android:id="@+id/ViewAnimator"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:padding="10dp" />  
  13.   
  14.     <LinearLayout  
  15.         android:orientation="horizontal"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:id="@+id/linearLayout1"  
  19.         android:gravity="center"  
  20.         android:minWidth="25px"  
  21.         android:minHeight="25px">  
  22.   
  23.         <Button  
  24.             android:id="@+id/btnNext"  
  25.             android:background="#055"  
  26.             android:text="NEXT"  
  27.             android:textColor="#fff"  
  28.             android:textStyle="bold"  
  29.             android:layout_width="wrap_content"  
  30.             android:layout_height="match_parent" />  
  31.         <Button  
  32.             android:id="@+id/btnPrevious"  
  33.             android:background="#055"  
  34.             android:text="Previous"  
  35.             android:textColor="#fff"  
  36.             android:textStyle="bold"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_marginLeft="150dp"  
  39.             android:layout_height="match_parent" />  
  40.     </LinearLayout>  
  41. </LinearLayout>  

Screenshot of the layout

 

I have taken 5 images here to show the sliding effect when the user clicks on “Next” or “Previous” button. Thus, I have added 5 image files in Resource->drawable folder. It is shown below.

 

Step 3

Implementation of ViewAnimator in MainActivity

Now, we need to use layout given above in MainActivity.cs file. I have taken layout given above as Main layout, using SetContentView() method. For smooth switches between two or more views, ViewAnimator have base methods, which I have used below to describe. 

This method does the opposite of SetInAnimation() method. This method first removes an old view, using an animation and then place a new view by SetInAnimation() method.

Method

Use

AddView()

As described before, ViewAnimator has two or more views to show. For it, this method is used to add views in ViewAnimator.

SetInAnimation()

This method is used to set an animation of view transition.

SetOutAnimation()

This method does opposite to SetInAnimation() method. This method first removes an old view, using an animation and then place new view by SetInAnimation() method.

ShowNext()

This method is used to show next view in ViewAnimator.

ShowPrevious()

This method is used to show previous view in ViewAnimator.

SetAnimateFirstView() 

This method is used to indicate the current view, which should animated the first time; when displayed in ViewAnimator by setting true or false value. Here, we set it as true, so it will be animated first time; when view is displayed in ViewAnimator.

RemoveView()

This method is used to remove any view from ViewAnimator.

There are many methods in ViewAnimator. Here, I have described some of them.

MainActivity.cs

  1. public class MainActivity : Activity  
  2.     {  
  3.         private ViewAnimator firstViewAnimator;  
  4.         Button btnNext,btnback;  
  5.         int[] images = {  
  6.         Resource.Drawable.image1,  
  7.         Resource.Drawable.image2,  
  8.         Resource.Drawable.image3,  
  9.         Resource.Drawable.image4,  
  10.         Resource.Drawable.image5  
  11.     };  
  12.         protected override void OnCreate(Bundle bundle)  
  13.         {  
  14.             base.OnCreate(bundle);  
  15.   
  16.             // Set our view from the "main" layout resource  
  17.              SetContentView (Resource.Layout.Main);  
  18.             btnNext = (Button)FindViewById(Resource.Id.btnNext);  
  19.             btnback = (Button)FindViewById(Resource.Id.btnPrevious);  
  20.             firstViewAnimator = (ViewAnimator)FindViewById(Resource.Id.ViewAnimator);  
  21.             for (int i = 0; i < images.Length; i++)  
  22.             {  
  23.                 ImageView imageView = new ImageView(this);  
  24.                 imageView.SetImageResource(images[i]);  
  25.                 firstViewAnimator.AddView(imageView);  
  26.             }     
  27.             firstViewAnimator.SetAnimateFirstView(true);  
  28.   
  29.             btnNext.Click += BtnNext_Click;  
  30.             btnback.Click += Btnback_Click;  
  31.         }  
  32.   
  33.         private void Btnback_Click(object sender, System.EventArgs e)  
  34.         {  
  35.             firstViewAnimator.SetOutAnimation(this, Android.Resource.Animation.SlideOutRight);  
  36.             firstViewAnimator.SetInAnimation(this, Android.Resource.Animation.SlideInLeft);  
  37.             firstViewAnimator.ShowPrevious();  
  38.         }  
  39.   
  40.         private void BtnNext_Click(object sender, System.EventArgs e)  
  41.         {  
  42.             firstViewAnimator.SetInAnimation(this, Android.Resource.Animation.SlideInLeft);  
  43.             firstViewAnimator.SetOutAnimation(this, Android.Resource.Animation.SlideOutRight);  
  44.             firstViewAnimator.ShowNext();  
  45.         }  
  46. }  

Here, I have taken 5 images as view and it is set by view in ViewAnimator object, using AddView() method. When the user clicks on “Next” button, the animation effect is set as view in from left and out in right side. It is opposite in nature, when the user clicks Previous button.

Output

 

Summary

In this article, we learned how to create an animation, using ViewAnimator. Methods of ViewAnimator and set animation effects are done , using ViewAnimator in Xamarin with Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all