Animations in Android application

Animations to your app interface will give high quality feel to your android applications. Animations can be performed through either XML or android code. In this tutorial i explained how to do animations using XML notations. I will explain how to do the same using android java code in future tutorials. Here i covered basic android animations like fade in, fade out, scale, rotate, slide up, slide down etc.,

1. MainActivity.java

package com.tutorialsee;

import com.tutorialsee.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

Button btnFadeIn, btnFadeOut, btnCrossFade, btnBlink, btnZoomIn,
btnZoomOut, btnRotate, btnMove, btnSlideUp, btnSlideDown,
btnBounce, btnSequential, btnTogether;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnFadeIn = (Button) findViewById(R.id.btnFadeIn);
btnFadeOut = (Button) findViewById(R.id.btnFadeOut);
btnCrossFade = (Button) findViewById(R.id.btnCrossFade);
btnBlink = (Button) findViewById(R.id.btnBlink);
btnZoomIn = (Button) findViewById(R.id.btnZoomIn);
btnZoomOut = (Button) findViewById(R.id.btnZoomOut);
btnRotate = (Button) findViewById(R.id.btnRotate);
btnMove = (Button) findViewById(R.id.btnMove);
btnSlideUp = (Button) findViewById(R.id.btnSlideUp);
btnSlideDown = (Button) findViewById(R.id.btnSlideDown);
btnBounce = (Button) findViewById(R.id.btnBounce);
btnSequential = (Button) findViewById(R.id.btnSequential);
btnTogether = (Button) findViewById(R.id.btnTogether);

/* Buttons click events */
// fade in
btnFadeIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, FadeInActivity.class);
startActivity(i);
}
});

// fade out
btnFadeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, FadeOutActivity.class);
startActivity(i);
}
});

// cross fade
btnCrossFade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,
CrossfadeActivity.class);
startActivity(i);
}
});

// blink
btnBlink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, BlinkActivity.class);
startActivity(i);
}
});

// Zoom In
btnZoomIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ZoomInActivity.class);
startActivity(i);
}
});

// Zoom Out
btnZoomOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ZoomOutActivity.class);
startActivity(i);
}
});

// Rotate
btnRotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, RotateActivity.class);
startActivity(i);
}
});

// Move
btnMove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MoveActivity.class);
startActivity(i);
}
});

// Slide Up
btnSlideUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SlideUpActivity.class);
startActivity(i);
}
});

// Slide Down
btnSlideDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,
SlideDownActivity.class);
startActivity(i);
}
});

// Slide Down
btnBounce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, BounceActivity.class);
startActivity(i);
}
});

// Sequential
btnSequential.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,
SequentialActivity.class);
startActivity(i);
}
});

// Together
btnTogether.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,
TogetherActivity.class);
startActivity(i);
}
});
}
}

2. activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#F9F9F9" >
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="#F9F9F9" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="#F9F9F9" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#F9F9F9"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnBounce"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:padding="22dp"
                android:text="Bounce"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnSequential"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:padding="22dp"
                android:text="Sequential Animation"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnFadeIn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="1dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Fade In"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/btnFadeOut"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="2dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Fade Out"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnCrossFade"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="1dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Cross Fade"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/btnBlink"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Blink"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnZoomIn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="1dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Zoom In"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/btnZoomOut"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Zoom Out"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnRotate"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="1dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Rotate"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/btnMove"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Move"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnSlideUp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="1dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Slide Up"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/btnSlideDown"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:text="Slide Down"
                android:textColor="#ffffff" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F9F9F9"
                   android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnTogether"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:background="#E91E63"
                android:padding="22dp"
                android:text="Together Animation"
                android:textColor="#ffffff" />
        </TableRow>
    </TableLayout>

</ScrollView>
</RelativeLayout>