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

[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 Play Store you...

OnePlus 7T vs iPhone 11 | Specs Comparison

The OnePlus 7T and iPhone 11 are the two best phones if you looking for a price of less than $1000. Both the phone...

Elon Musk unveils Tesla’s Cybertruck

Are you into gaming or love watching sci-fi movies?  What about those back to the future movies which gave us a glimpse of the...

7 Best Hiking Apps you Must Try in 2020

Having the best hiking apps always gives you an advantage if you are going for adventurous trips. These apps can track your workout and activities...

[UPDATED] 9 Best Android Launcher Apps of 2019

Android launcher is best to customize the overall experience of Android for a user in terms of look and act. In apps store, there are...

Related Stories

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay on Top - Get the daily news in your inbox