Skip to content
Related Articles

Related Articles

Improve Article

How to create AnimatedGradient in Android?

  • Last Updated : 25 Jun, 2020

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.


    Gradient Drawable

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

    My Personal Notes arrow_drop_up
  • Recommended Articles
    Page :