Open In App
Related Articles

How to create AnimatedGradient in Android?

Improve Article
Save Article
Like Article

In this article, we are going to learn how to create AnimatedGradient in android. It can be used in the background of our app. In this, we add different color gradients and animate them. Even in the older versions of Instagram, the developers used AnimatedGradient for the background of the login screen to make the layout look attractive.

Step 1: Create different gradient xml files which are to be changed in the background. These files include startColor which is shown in above part, endColor which is shown in lower part and angle of the horizontal line which divides both the part. Here we create three gradient xml files.

  1. Create a gradient_one.xml file in the drawable folder and add the following code.


    <?xml version="1.0" encoding="utf-8"?>
            android:startColor="#ffd6d3" />


  2. Create a gradient_two.xml file in the drawable folder and add the following code.


    <?xml version="1.0" encoding="utf-8"?>
            android:startColor="#A4DDF8" />


  3. Create a gradient_three.xml file in the drawable folder and add the following code.


    <?xml version="1.0" encoding="utf-8"?>
            android:startColor="#F6F8F0" />


Step 2: Create a gradient_list.xml file in the drawable folder and add the following code. In this file we add all our gradient xml files that we want to display and their duration.


<?xml version="1.0" encoding="utf-8"?>
    <item android:drawable="@drawable/first_gradient"
    <item android:drawable="@drawable/third_gradient"
    <item android:drawable="@drawable/sec_gradient"

Step 3: Now add the following code in the activity_main.xml file. In this file we add our gradient_list to the background of our layout.


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintTop_toTopOf="parent" />

Step 4: Now add the following code in the file. From the class AnimatedDrawable, functions such as setEnterFadeDuration and setExitFadeDuration are used to set fading duration and finally start function is used to start the animation.

package org.geeksforgeeks.gfganimatedGradient;
package org.geeksforgeeks.gfganimatedGradient;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        ConstraintLayout layout = findViewById(;
        //With the help of AnimatedDrawable class, we can set
        //the duration to our background and then call the
        //function start at the end.
        AnimationDrawable animationDrawable = (AnimationDrawable) 

  • Now compile and run the Android app.
  • Output:

    Gradient Drawable

    Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

    Last Updated : 25 Jun, 2020
    Like Article
    Save Article
    Similar Reads
    Complete Tutorials