Search code examples
c#androidxamarintitanium-android

How can I use animation in xamarin android application?


I want to use animation in my android Application using Xamarin C#. animations like fade-in, zoom-in, move and ....


Solution

  • first add a folder under "resources " folder name it "anim". then you can add your animation resources to it , Ex: for fade-in animation create a resource under anim folder and name it "fade_in.xml" and paste this code into it:

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
        android:fillAfter="true" >
    
        <alpha
            android:duration="1000"
            android:fromAlpha="0.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="1.0" />
    
    </set>
    

    then add a Textview in your mainlayout.xml and also a button

     <TextView
                android:text="Text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/txtMessage"
                android:layout_marginBottom="35.3dp" />
    

    and for button:

    <Button
                    android:text="fade in"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/fadein" />
    

    in "oncreate" method in you activity add this code :

      Button fadein = FindViewById<Button>(Resource.Id.fadein);
                fadein.Click += btn_Click;
    

    then add this method to your activity:

    void blink_Click(object sender, EventArgs e)
            {
                txtMessage = FindViewById<TextView>(Resource.Id.txtMessage);
                 Button b = sender as Button;
                Animation anim = AnimationUtils.LoadAnimation(ApplicationContext,
                               Resource.Animation.fade_in);
                 txtMessage.StartAnimation(anim);
            }