In this tutorial, we will learn to change the background transition in Android Studio. We will be using AnimationDrawable for this project.

How to change the background transition in Android Steps:

MainActivity.java

In this, we are adding the AnimationDrawable to the layout ID which we have set on the XML file. setEnterFadeDuration will start the fade animation. Then, we will call start() to run the animation.

public class MainActivity extends AppCompatActivity {

    private ConstraintLayout layout;


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

        layout = findViewById(R.id.layout);

        AnimationDrawable animationDrawable = (AnimationDrawable) layout.getBackground();
        animationDrawable.setEnterFadeDuration(2000);
        animationDrawable.setExitFadeDuration(3000);
        animationDrawable.start();
    }


}

Acitivity_main.xml

We have defined all the background layout we want in our apps in the drawable folder as background_list.xml. In this, we define a simple button over the background image.

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/background_list"
    android:id="@+id/layout"
    >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_shape"
        android:paddingHorizontal="25dp"
        android:text="HELLO!"
        android:textSize="22dp"
    />

</android.support.constraint.ConstraintLayout>

Background_list.xml

In this XML, we start tag with <animation-list> and a series of nested <item>tags. These items are having the image name which is in the drawable folder and we cant set the duration also of each item.

<animation-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:drawable="@drawable/image-one"
    android:duration="6000"/>

    <item
        android:drawable="@drawable/image-two"
        android:duration="6000"/>

    <item
    android:drawable="@drawable/image-three"
    android:duration="6000"/>
</animation-list>