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

28 Responses

  1. Vikrant Shah

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

    Reply
      • Vikrant Shah

        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

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

      • Vikrant Shah

        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

        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

        Menifest File :

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

  2. Alex

    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..

    Reply
    • wp_sethi

      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.

      Reply
    • wp_sethi

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

      Reply
  3. Manpreet

    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?

    Reply

Leave a Reply

Your email address will not be published.

4 + seventeen =