Register Login

Fade in Fade out Animation Effect to Android App

Updated May 23, 2019

How to Add Fade in Fade out Animation to Android App

This tutorial explains how to add fade in and fade out animation in Android Application.

Please follow the steps below in order to add fade in fade out animation to Android Application:

1.First add a new handler. It will open the application

And after 4 seconds open the second activity as the 'welcome' timeout = 4000.

2.Go to res > New > Directory.

3.Go to anim > New > Animation resource file.

4.Fade_in.xml and fade_out.xml has been added.

5.Click on Fade_in.xml.

6.Click on fade_out.xml.

7.Go to MainActivity.java. Add a line of code overridePendingTransition().

8.Go to activity_main.xml.

9.Go to activity_second.xml.

10.Run it on the live device.

Complete Code Fade in Fade out Animation Effect to Android App

app>java>com.sabhithpkcmnr.screenonapp.MainActivity.java

package com.sabhithpkcmnr.screenonapp;
import ...
public class MainActivity extends AppCompatActivity {
    private static int WELCOME_TIMEOUT = 4000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);;
        setContentView(R.layout.activity_main);
        new Handler().postDelayed(new Runnable () {
            @Override
            public void run() {
                Intent welcome = new Intent (MainActivity.this, SecondActivity.class);
                startActivity(welcome);
                overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
                finish();
            }
        }, WELCOME_TIMEOUT);
    }
}

app>java>com.sabhithpkcmnr.screenonapp.SecondActivity.java

package com.sabhithpkcmnr.screenonapp;
import ...
public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle SavedInstanceState){
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.activity_second);        
    }
}

app>res>anim>fadein.xml

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

app>res>anim>fadeout.xml

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

 


×