How to change the background transition in Android Studio

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>

Recent Articles

8 Best Offline Football Games for Android 2020

Are you looking for the best offline football games? Football is one of the world's most popular games. Even it is one...

Samsung Galaxy S20+ leaked, image surface online

Just a few days beofre Samsung is going to showcase the new S20 series, Galaxy S20+ leaked images online somehow showing the...

Samsung Galaxy Note 10 lite: 2 Year old processor smartphone, Best Mid ranger?

Earlier this week Samsung unveiled Samsung Note 10 lite and Samsung Galaxy S10 lite. It was very surprising as Galaxy lineup has...

Easy Trick: How to Merge Finder windows in Mac

If you have used old MAC OS then you might remember you can't merge the windows and it was very difficult to work...

[Updated] Temporarily Free Games and Apps on Google Play Store

Every week there are free games and apps on the Google Play store. Here is the list of free games and apps on the...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay on Top - Get the daily news in your inbox