This is a quick tutorial on how to make a moving gradient background on Android.

To achieve this all we need is AnimationList. Lets start!

First we will create five gradient drawables in different files, as follows.

Now in a new xml drawable file add the following code that contains the AnimationList which will be responsible to change the background from one gradient to the other. Inside the AnimationList tag, add 5 items which are referring to the above 5 gradient files using the android:drawable=”@drawable/name” tag.

Now set it as a background to the root View/ViewGroup of the activity that you want to have moving background. Also don’t forget to give the ViewGroup/View an id that you are applying the background to, as we will need to referrer it in our Java code.

Now all we have to do is in our Java code tell the animation list to start transition between drawables. That we can do by calling the AnimationDrawbale.start() method as follows

As you can see, we refer to the LinearLayout that has the animation list as background. Then we get the background from it in a AnimationDrawbale. Then we set the exit and enter duration of a single gradient and start it. Pretty simple!

You can play with the exit and enter duration and set it to what you feel works better. Also you can add as much as gradients as you want. Here is a great website to find gradient combinations : UiGradients

gradient


Gurleen Sethi

Android Developer and enthusiast, believes in developing products and content to help people. Likes to do deep thinking.

28 Comments

Barack · April 7, 2017 at 3:19 pm

Could you make gif with working process?

LeeHounshell · April 10, 2017 at 4:04 am

Nice article. Thanks!

Mohsin · April 10, 2017 at 10:07 am

Its very helpful article. Many Thanks.

Murli · April 10, 2017 at 10:46 am

Very good article, it will be very helpful,Ver thanks.

Keyur · April 10, 2017 at 10:52 am

Nice

Alfishan · April 10, 2017 at 12:05 pm

For dynamic gradient Background check this https://github.com/Alfishan/GradientBackgound

    Chintan Rathod · April 10, 2017 at 7:50 am

    You should add how to use.

Vikrant Shah · April 10, 2017 at 4:29 pm

will throw runtimeException :- android.view.InflateException:Error inflating class android.widget.RelativeLayout.

    wp_sethi · April 10, 2017 at 11:19 am

    Could you provide more info/code ?

      Vikrant Shah · April 11, 2017 at 6:28 am

      Application crash at OnCreate of Activity.
      Error shows at setContentView() method.
      FATAL EXCEPTION: main
      Process:com.android.Test, PID: 81632
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.Test/com.android.Test.TestActivity}:
      android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class
      at com.android.Test.TestActivity.onCreate(TestActivity.java:16)

        wp_sethi · April 11, 2017 at 6:30 am

        You have a problem in you activity xml at line 2, could you post your xml?

          Vikrant Shah · April 11, 2017 at 6:44 am

          AndroidManifest.xml :
          ————————–

          ___________________________________

          activity_test.xml:
          ——————–

          ___________________________________
          TestActivity.java
          —————–

          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          try{
          setContentView(R.layout.activity_test);
          layoutTest = (RelativeLayout) findViewById(R.id.layoutTest);
          AnimationDrawable animationDrawable = (AnimationDrawable) layoutTest.getBackground();

          animationDrawable.setEnterFadeDuration(2500);
          animationDrawable.setExitFadeDuration(5000);

          animationDrawable.start();
          }catch (Exception e){
          Log.e(TAG, “onCreate: “+e.getMessage());
          }
          }
          _______________________________________________________
          Now coz, of try-catch block: the exception message :- E/TestActivity: onCreate: Binary XML file line #2: Binary XML file line #2: Error inflating class

          wp_sethi · April 11, 2017 at 6:46 am

          You have some problem in you activity_test.xml file, post your project on github and give me the link, I will take a look at it.

          Vikrant Shah · April 11, 2017 at 6:46 am

          <RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android&quot;
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/animation_list"
          android:id="@+id/layoutTest"

          Vikrant Shah · April 11, 2017 at 6:47 am

          Menifest File :

          android:name=”.TestActivity”
          android:theme=”@style/myAppCompat”

Alex · April 10, 2017 at 11:42 pm

Does this use a lot of resources if kept running for a little? I mean I would use this background on a main UI where users could be spending about 30-90s so it should be okay..

    wp_sethi · April 10, 2017 at 6:14 pm

    I am not really sure if it would take too much resources, I doubt that it should, Instagram has been doing this with their logon screen in the past. The better option is try out, run the app and check the system load in the cpu profiler.

payam · April 11, 2017 at 1:50 am

Tried it for my app and it was amazing. Thanks.
By the way, I translated your article (with a link to the source) and its in here :
https://kivee.ir/2017/04/%D8%A7%DB%8C%D8%AC%D8%A7%D8%AF-%DB%8C%DA%A9-gradient-background-%D9%85%D8%AA%D8%AD%D8%B1%DA%A9.html

    wp_sethi · April 11, 2017 at 6:31 am

    Nice.

AndroidDeveloperLB · April 11, 2017 at 12:41 pm

Is there a github repo with this sample?

    wp_sethi · April 11, 2017 at 7:13 am

    I haven’t created a repo for this, but all the code that you will need is available in the post.

Manpreet · April 13, 2017 at 4:07 pm

Any way I can set this to auto start? I mean, any way I can skip getting the background from the LinearLayout and simply force it to start on its own?

Gunvant Murge · April 14, 2017 at 4:47 pm

Very Nice sir thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × two =