[Guide] Adding Animations To Your Application [Part 1]

Search This thread

Saurabh Shah

Senior Member
Jun 15, 2013
115
169
Bangalore
www.thehackwall.com
Adding Aminations To Give Your Application A Cool Strike :good:

Here I have mentioned some Cool Animations Effects.


[Note: Assuming the package name to be package.name.here and created a folder named anim in /res/.

Blink Effect
Code:
[QUOTE]
[B]Java Code[/B] [BlinkActivity.java]

package package.name.here;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class BlinkActivity extends Activity implements AnimationListener {

	TextView txtMessage;
	Button btnStart;

	// Animation
	Animation animBlink;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_blink);

		txtMessage = (TextView) findViewById(R.id.txtMessage);
		btnStart = (Button) findViewById(R.id.btnStart);

		// load the animation
		animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.blink);
		
		// set animation listener
		animBlink.setAnimationListener(this);

		// button click event
		btnStart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				txtMessage.setVisibility(View.VISIBLE);
				
				// start the animation
				txtMessage.startAnimation(animBlink);
			}
		});

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// Take any action after completing the animation

		// check for blink animation
		if (animation == animBlink) {
		}

	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}

	@Override
	public void onAnimationStart(Animation animation) {

	}

}


[B]XML Code[/B] [activity_blink.xml in [COLOR="green"]res/layout/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">    
    
    <TextView android:id="@+id/txtMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is fadein animation"
        android:layout_centerInParent="true"
        android:textSize="25dp"
        android:padding="20dp"
        android:background="#000000"
        android:visibility="gone"/>
        
    <Button android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:layout_marginTop="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

[B]XML Code[/B]  [Blink.xml in [COLOR="green"]res/anim/[/COLOR] folder]

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



[/QUOTE]

FadeOut Effect

Code:
[QUOTE]
[B]Java Code[/B]  [FadeOutActivity.java]

package info.androidhive.androidanimations;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FadeOutActivity extends Activity implements AnimationListener {

	TextView txtMessage;
	Button btnStart;

	// Animation
	Animation animFadeOut;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fadeout);

		txtMessage = (TextView) findViewById(R.id.txtMessage);
		btnStart = (Button) findViewById(R.id.btnStart);

		// load the animation
		animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.fade_out);

		// set animation listener
		animFadeOut.setAnimationListener(this);

		// button click event
		btnStart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// start the animation
				txtMessage.startAnimation(animFadeOut);
			}
		});

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// Take any action after completing the animation

		// check for fade out animation
		if (animation == animFadeOut) {
			Toast.makeText(getApplicationContext(), "Animation Stopped",
					Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}

}
[B]XML Code[/B] [activity_fadeout.xml in [COLOR="green"]res/layout/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView android:id="@+id/txtMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is fadeout animation"
        android:layout_centerInParent="true"
        android:textSize="25dp"/> 
        
    <Button android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Animation"
        android:layout_marginTop="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

[B]XML Code[/B] [fade_out.xml in [COLOR="green"]res/anim[/COLOR] folder]

<?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="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

</set>

[/QUOTE]

CrossFade Effect

Code:
[QUOTE]
[B]Java Code[/B] [CrossFadeActivity.java]
package package.name.here;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class CrossfadeActivity extends Activity implements AnimationListener {

	TextView txtMessage1, txtMessage2;
	Button btnStart;

	// Animation
	Animation animFadeIn, animFadeOut;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_crossfade);

		txtMessage1 = (TextView) findViewById(R.id.txtMessage1);
		txtMessage2 = (TextView) findViewById(R.id.txtMessage2);
		btnStart = (Button) findViewById(R.id.btnStart);

		// load animations
		animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.fade_in);
		animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.fade_out);

		// set animation listeners
		animFadeIn.setAnimationListener(this);
		animFadeOut.setAnimationListener(this);

		// button click event
		btnStart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// make fade in element visible
				txtMessage2.setVisibility(View.VISIBLE);
				// start fade in animation
				txtMessage2.startAnimation(animFadeIn);
				
				// start fade out animation
				txtMessage1.startAnimation(animFadeOut);
			}
		});

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// Take any action after completing the animation

		// if animation is fade out hide them after completing animation
		if (animation == animFadeOut) {
			
			// hide faded out element
			txtMessage1.setVisibility(View.GONE);
		}
		
		if(animation == animFadeIn){
			// do something after fade in completed
			
			// set visibility of fade in element
			txtMessage2.setVisibility(View.VISIBLE);
		}

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}

}

[B]XML Code[/B] [activity_crossfade.xml in [COLOR="green"]res/layout/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView android:id="@+id/txtMessage1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This text will fade out"
        android:layout_centerInParent="true"
        android:textSize="25dp"/> 
        
    <TextView android:id="@+id/txtMessage2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This text will fade in"
        android:layout_centerInParent="true"
        android:textSize="25dp"
        android:visibility="gone"/> 
        
    <Button android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Animation"
        android:layout_marginTop="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

[B]NOTE:No need to create any xml in anim folder for crossfade because it uses fadein and fadeout xml files from anim folder][/B]
[/QUOTE]

FadeIn Effect

Code:
[QUOTE]
[B]Java Code[/B] [FadeInActivity.java]

package package.name.here;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class FadeInActivity extends Activity implements AnimationListener {

	TextView txtMessage;
	Button btnStart;

	// Animation
	Animation animFadein;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fadein);

		txtMessage = (TextView) findViewById(R.id.txtMessage);
		btnStart = (Button) findViewById(R.id.btnStart);

		// load the animation
		animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
				R.anim.fade_in);
		
		// set animation listener
		animFadein.setAnimationListener(this);

		// button click event
		btnStart.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				txtMessage.setVisibility(View.VISIBLE);
				
				// start the animation
				txtMessage.startAnimation(animFadein);
			}
		});

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// Take any action after completing the animation

		// check for fade in animation
		if (animation == animFadein) {
			Toast.makeText(getApplicationContext(), "Animation Stopped",
					Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}

}

[B]XML Code[/B] [activity_fadein.xml in [COLOR="green"]res/layout/[/COLOR] folder]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView android:id="@+id/txtMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is fadein animation"
        android:layout_centerInParent="true"
        android:textSize="25dp"
        android:visibility="gone"/> 
        
    <Button android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Animation"
        android:layout_marginTop="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

[B]XML Code[/B] [fade_in.xml in [COLOR="green"]res/anim/[/COLOR] folder]

<?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>

[/QUOTE]


Also Check Out Adding Animations To Your Application [Part 2]:victory:

Please correct me if the code goes wrong somewhere.


Saurabh Shah

----------------------------------------------------------------

Hit Thanks :good:
 
Last edited:

yvesadriel

Member
Dec 2, 2012
10
0
Philippines
Adding Aminations To Give Your Application A Cool Strike :good:

Here I have mentioned some Cool Animations Effects.


Blink Effect
Code:

FadeOut Effect

Code:

CrossFade Effect

Code:

FadeIn Effect

Code:


More Effects coming soon on my next Post :victory: .

Please correct me if the code goes wrong somewhere.


A-ssassi-N


----------------------------------------------------------------

Hit Thanks :good:
nice :good: :cowboy::cowboy:
 

Wetzel402

Senior Member
I use these animations in my app.

Slide in left:
Code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="-100%p" android:toXDelta="0%p" android:duration="250" />
</set>

Slide in right:
Code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="100%p" android:toXDelta="0%p" android:duration="250" />
</set>

Slide out left:
Code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="0%p" android:toXDelta="-100%p" android:duration="250" />
</set>

Slide out right:
Code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
    <translate android:fromXDelta="0%p" android:toXDelta="100%p" android:duration="250" />
</set>

I call them like so in base activity:
Code:
void slideInLeft(final View view) {
		leftAnim = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
		leftAnim.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(final Animation animation) {

			}

			@Override
			public void onAnimationRepeat(final Animation animation) {

			}

			@Override
			public void onAnimationEnd(final Animation animation) {
			}
		});

		view.startAnimation(leftAnim);
	}
 
Last edited:

Saurabh Shah

Senior Member
Jun 15, 2013
115
169
Bangalore
www.thehackwall.com
Sorry, do not understand how to use your codes? :confused:

Choose the animation you would like to use for you application.

Create a Android application project with blank activity.

[Note: Assuming that your package name is example.package]

Copy the Java Code to src/example.package/MainActivity.java

Copy the XML Code to res/layout/activity_main.xml

BUILD. DONE.


Saurabh Shah

----------------------------------------------------------------

Hit Thanks :good:
 
Last edited:

Saurabh Shah

Senior Member
Jun 15, 2013
115
169
Bangalore
www.thehackwall.com
Last edited:

Dr.Alexander_Breen

Senior Member
Jun 17, 2012
439
1,072
You have to add Property Animations(and NineOldAndroids library), they are really significant to animating applications nowadays.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 26
    Adding Aminations To Give Your Application A Cool Strike :good:

    Here I have mentioned some Cool Animations Effects.


    [Note: Assuming the package name to be package.name.here and created a folder named anim in /res/.

    Blink Effect
    Code:
    [QUOTE]
    [B]Java Code[/B] [BlinkActivity.java]
    
    package package.name.here;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.Animation.AnimationListener;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class BlinkActivity extends Activity implements AnimationListener {
    
    	TextView txtMessage;
    	Button btnStart;
    
    	// Animation
    	Animation animBlink;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_blink);
    
    		txtMessage = (TextView) findViewById(R.id.txtMessage);
    		btnStart = (Button) findViewById(R.id.btnStart);
    
    		// load the animation
    		animBlink = AnimationUtils.loadAnimation(getApplicationContext(),
    				R.anim.blink);
    		
    		// set animation listener
    		animBlink.setAnimationListener(this);
    
    		// button click event
    		btnStart.setOnClickListener(new View.OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				txtMessage.setVisibility(View.VISIBLE);
    				
    				// start the animation
    				txtMessage.startAnimation(animBlink);
    			}
    		});
    
    	}
    
    	@Override
    	public void onAnimationEnd(Animation animation) {
    		// Take any action after completing the animation
    
    		// check for blink animation
    		if (animation == animBlink) {
    		}
    
    	}
    
    	@Override
    	public void onAnimationRepeat(Animation animation) {
    
    	}
    
    	@Override
    	public void onAnimationStart(Animation animation) {
    
    	}
    
    }
    
    
    [B]XML Code[/B] [activity_blink.xml in [COLOR="green"]res/layout/[/COLOR] folder]
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">    
        
        <TextView android:id="@+id/txtMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is fadein animation"
            android:layout_centerInParent="true"
            android:textSize="25dp"
            android:padding="20dp"
            android:background="#000000"
            android:visibility="gone"/>
            
        <Button android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start"
            android:layout_marginTop="30dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"/>
    
    </RelativeLayout>
    
    [B]XML Code[/B]  [Blink.xml in [COLOR="green"]res/anim/[/COLOR] folder]
    
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <alpha android:fromAlpha="0.0"
            android:toAlpha="1.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:duration="600"
            android:repeatMode="reverse"
            android:repeatCount="infinite"/>
    </set>
    
    
    
    [/QUOTE]

    FadeOut Effect

    Code:
    [QUOTE]
    [B]Java Code[/B]  [FadeOutActivity.java]
    
    package info.androidhive.androidanimations;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.view.animation.Animation.AnimationListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class FadeOutActivity extends Activity implements AnimationListener {
    
    	TextView txtMessage;
    	Button btnStart;
    
    	// Animation
    	Animation animFadeOut;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_fadeout);
    
    		txtMessage = (TextView) findViewById(R.id.txtMessage);
    		btnStart = (Button) findViewById(R.id.btnStart);
    
    		// load the animation
    		animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
    				R.anim.fade_out);
    
    		// set animation listener
    		animFadeOut.setAnimationListener(this);
    
    		// button click event
    		btnStart.setOnClickListener(new View.OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				// start the animation
    				txtMessage.startAnimation(animFadeOut);
    			}
    		});
    
    	}
    
    	@Override
    	public void onAnimationEnd(Animation animation) {
    		// Take any action after completing the animation
    
    		// check for fade out animation
    		if (animation == animFadeOut) {
    			Toast.makeText(getApplicationContext(), "Animation Stopped",
    					Toast.LENGTH_SHORT).show();
    		}
    
    	}
    
    	@Override
    	public void onAnimationRepeat(Animation animation) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void onAnimationStart(Animation animation) {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    [B]XML Code[/B] [activity_fadeout.xml in [COLOR="green"]res/layout/[/COLOR] folder]
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <TextView android:id="@+id/txtMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is fadeout animation"
            android:layout_centerInParent="true"
            android:textSize="25dp"/> 
            
        <Button android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start Animation"
            android:layout_marginTop="30dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"/>
    
    </RelativeLayout>
    
    [B]XML Code[/B] [fade_out.xml in [COLOR="green"]res/anim[/COLOR] folder]
    
    <?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="1.0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:toAlpha="0.0" />
    
    </set>
    
    [/QUOTE]

    CrossFade Effect

    Code:
    [QUOTE]
    [B]Java Code[/B] [CrossFadeActivity.java]
    package package.name.here;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.Animation.AnimationListener;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class CrossfadeActivity extends Activity implements AnimationListener {
    
    	TextView txtMessage1, txtMessage2;
    	Button btnStart;
    
    	// Animation
    	Animation animFadeIn, animFadeOut;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_crossfade);
    
    		txtMessage1 = (TextView) findViewById(R.id.txtMessage1);
    		txtMessage2 = (TextView) findViewById(R.id.txtMessage2);
    		btnStart = (Button) findViewById(R.id.btnStart);
    
    		// load animations
    		animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
    				R.anim.fade_in);
    		animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(),
    				R.anim.fade_out);
    
    		// set animation listeners
    		animFadeIn.setAnimationListener(this);
    		animFadeOut.setAnimationListener(this);
    
    		// button click event
    		btnStart.setOnClickListener(new View.OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				// make fade in element visible
    				txtMessage2.setVisibility(View.VISIBLE);
    				// start fade in animation
    				txtMessage2.startAnimation(animFadeIn);
    				
    				// start fade out animation
    				txtMessage1.startAnimation(animFadeOut);
    			}
    		});
    
    	}
    
    	@Override
    	public void onAnimationEnd(Animation animation) {
    		// Take any action after completing the animation
    
    		// if animation is fade out hide them after completing animation
    		if (animation == animFadeOut) {
    			
    			// hide faded out element
    			txtMessage1.setVisibility(View.GONE);
    		}
    		
    		if(animation == animFadeIn){
    			// do something after fade in completed
    			
    			// set visibility of fade in element
    			txtMessage2.setVisibility(View.VISIBLE);
    		}
    
    	}
    
    	@Override
    	public void onAnimationRepeat(Animation animation) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void onAnimationStart(Animation animation) {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    
    [B]XML Code[/B] [activity_crossfade.xml in [COLOR="green"]res/layout/[/COLOR] folder]
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <TextView android:id="@+id/txtMessage1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This text will fade out"
            android:layout_centerInParent="true"
            android:textSize="25dp"/> 
            
        <TextView android:id="@+id/txtMessage2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This text will fade in"
            android:layout_centerInParent="true"
            android:textSize="25dp"
            android:visibility="gone"/> 
            
        <Button android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start Animation"
            android:layout_marginTop="30dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"/>
    
    </RelativeLayout>
    
    [B]NOTE:No need to create any xml in anim folder for crossfade because it uses fadein and fadeout xml files from anim folder][/B]
    [/QUOTE]

    FadeIn Effect

    Code:
    [QUOTE]
    [B]Java Code[/B] [FadeInActivity.java]
    
    package package.name.here;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.view.animation.Animation.AnimationListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class FadeInActivity extends Activity implements AnimationListener {
    
    	TextView txtMessage;
    	Button btnStart;
    
    	// Animation
    	Animation animFadein;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		// TODO Auto-generated method stub
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_fadein);
    
    		txtMessage = (TextView) findViewById(R.id.txtMessage);
    		btnStart = (Button) findViewById(R.id.btnStart);
    
    		// load the animation
    		animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
    				R.anim.fade_in);
    		
    		// set animation listener
    		animFadein.setAnimationListener(this);
    
    		// button click event
    		btnStart.setOnClickListener(new View.OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				txtMessage.setVisibility(View.VISIBLE);
    				
    				// start the animation
    				txtMessage.startAnimation(animFadein);
    			}
    		});
    
    	}
    
    	@Override
    	public void onAnimationEnd(Animation animation) {
    		// Take any action after completing the animation
    
    		// check for fade in animation
    		if (animation == animFadein) {
    			Toast.makeText(getApplicationContext(), "Animation Stopped",
    					Toast.LENGTH_SHORT).show();
    		}
    
    	}
    
    	@Override
    	public void onAnimationRepeat(Animation animation) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void onAnimationStart(Animation animation) {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    
    [B]XML Code[/B] [activity_fadein.xml in [COLOR="green"]res/layout/[/COLOR] folder]
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <TextView android:id="@+id/txtMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is fadein animation"
            android:layout_centerInParent="true"
            android:textSize="25dp"
            android:visibility="gone"/> 
            
        <Button android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start Animation"
            android:layout_marginTop="30dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="20dp"/>
    
    </RelativeLayout>
    
    [B]XML Code[/B] [fade_in.xml in [COLOR="green"]res/anim/[/COLOR] folder]
    
    <?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>
    
    [/QUOTE]


    Also Check Out Adding Animations To Your Application [Part 2]:victory:

    Please correct me if the code goes wrong somewhere.


    Saurabh Shah

    ----------------------------------------------------------------

    Hit Thanks :good:
    4
    Sorry, do not understand how to use your codes? :confused:

    Choose the animation you would like to use for you application.

    Create a Android application project with blank activity.

    [Note: Assuming that your package name is example.package]

    Copy the Java Code to src/example.package/MainActivity.java

    Copy the XML Code to res/layout/activity_main.xml

    BUILD. DONE.


    Saurabh Shah

    ----------------------------------------------------------------

    Hit Thanks :good:
    3
    Nice guide! :good:

    but there are already a couple of open source animations available like PropertyAnimation or the Android3DFlipTransition for instance. Maybe you can include those, too!

    And also, you might want to make a section for animations in a list view as well (like the ListViewAnimations or the SugaredListAnimations)
    3
    Thanks

    Thanks for making this guide. I look forward to seeing more animations in the future. I've posted this guide to the XDA Portal. :)

    Thank you very much. :)
    2
    Nice guide! :good:

    but there are already a couple of open source animations available like PropertyAnimation or the Android3DFlipTransition for instance. Maybe you can include those, too!

    And also, you might want to make a section for animations in a list view as well (like the ListViewAnimations or the SugaredListAnimations)

    Android3DFlipTransition is Awesome man :D
    Thankyou for suggestion :)