How To Animate Background In Android Studio or Make Color Changing Background - ILoveDeshi

Yes, it is Possible to animate your activity or splash background to make animated or change color automatically using a simple method. You Can make Smooth Gradient Colors in Background too. Welcome to ILoveDeshi.Com

How To Animate Background In Android Studio or Make Color Changing Background - ILoveDeshi

How to Animate Activity Background Using Simple Drawables...

First of all Open Android Studio And Create A New Project Choose Empty Activity Layout Then Click on Next Enter Your Project Name "Animated Background" Choose Language "Java" Then Click on Finish.

What We Are Going To Do.

  1. Create two or more drawable files with different color gradients.
  2. Create one more another drawable with an animation-list tag and add all the created gradient drawable as the items.
  3. Use the animation-list drawable as the background of the view in the layout.
  4. Start the animation in the onCreate method of the Activity.

Create Drawable first.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="225"
        android:endColor="#9C27B0"
        android:startColor="#3F51B5" />
</shape>

Create Drawable second.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="225"
        android:endColor="#05a8f3"
        android:startColor="#3f52b6" />
</shape>

Now Create An Animation Drawable

Create an animate.xml drawable inside it, then we will define the items and set the first.xml and second.xml files as drawable.

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/first"
        android:duration="4000" />
    <item
        android:drawable="@drawable/second"
        android:duration="4000" />
</animation-list>

Using Animation-List Drawable

You can use the animate.xml as the background in any Activity.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/back"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/animate"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/this_is_animated_background"
        android:textColor="#FFF"
        android:textSize="25sp" />

</LinearLayout>

Now Start The Animation in Java of your MainActivity

In your activity or fragment class, you will have to start the animation and this can be done in your onCreate method as shown below.

public class MainActivity extends AppCompatActivity {

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

        LinearLayout background = findViewById(R.id.back);
        AnimationDrawable animationDrawable = (AnimationDrawable) background.getBackground();
        animationDrawable.setEnterFadeDuration(4000);
        animationDrawable.setExitFadeDuration(4000);
        animationDrawable.start();
    }
}