49

Has anyone noticed the SwipeRefreshLayout in gmail. It changes color for every rotation in a single load iteration. Does anyone have any idea how to achieve it?

1
  • 1
    You should show part of your code and not asking for a full code implementation. The way to achieve that is by using setColorScheme in the Swipe Refresh object. It allows you to set 4 colors. Commented Sep 8, 2015 at 14:48

6 Answers 6

78

The documentation for SwipeRefreshLayout details the setColorSchemeResources() and setColorSchemeColors() methods that can be used to set the color scheme of your SwipeRefreshLayout

The docs state:

Set the color resources used in the progress animation from color resources. The first color will also be the color of the bar that grows in response to a user swipe gesture.

You should note that setColorSchemeColors() should be used with specific colour values and setColorSchemeResources() should be used when you intend to use resource references to colours (e.g. R.color.red).

Please also note that setColorScheme(), the old way of completing this, is now deprecated.

8
  • In my case setColorSchemeColors() and setColorSchemeResources() only changes the color of arrow, ring progress color remains white Commented Oct 10, 2016 at 12:30
  • @RajeshKoshti As the docs state, this changes the progress animation color and not the view Commented Oct 10, 2016 at 12:32
  • then what should i do for change the color of progress ring? Commented Oct 10, 2016 at 12:34
  • @RajeshKoshti as far as I am aware, you can't Commented Oct 10, 2016 at 12:38
  • 8
    @RajeshKoshti To change the background of the swipe refresh circle you should use setProgressBackgroundColorSchemeResource() method. Better late than never) Commented Mar 17, 2018 at 13:07
31

Change colors as per UI requirement:

swipeRefreshLayout.setColorSchemeColors(Color.BLUE, Color.YELLOW, Color.BLUE);
4

Here is how you can get the colors from the colors.xml resource and set them as SwipRefreshLayout's color scheme.

int c1 = getResources().getColor(R.color.refresh_progress_1);
int c2 = getResources().getColor(R.color.refresh_progress_2);
int c3 = getResources().getColor(R.color.refresh_progress_3);

mySwipeRefreshLayout.setColorSchemeColors(c1, c2, c3);
3
swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.simpleSwipeRefreshLayout);

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
 {
            @Override
            public void onRefresh() {

                // implement Handler to wait for 3 seconds and then update UI means update value of TextView
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // cancle the Visual indication of a refresh
                        swipeRefreshLayout.setRefreshing(false);
                        swipeRefreshLayout.setColorSchemeResources(R.color.Aqua,R.color.Blue,R.color.Violet);
                        // Generate a random integer number
                        Random r = new Random();
                        int i1 = r.nextInt(80 - 65) + 65;
                        // set the number value in TextView
                        textView.setText(String.valueOf(i1));
                    }
                }, 3000);
            }
        });
    }
2
  • Please provide some explanation with your answer.
    – Partho63
    Commented Mar 9, 2019 at 12:11
  • I made xml with <android.support.v4.widget.SwipeRefreshLayout ...> and config in MainActivity.java,This above code generate random number when you pulldown screen with mullticolor progressdialog.This example is defined using swiperefresh layout. Commented Mar 9, 2019 at 13:44
-1

you can use any of those either passing Colors values or Colors Resource Ids

  • Passing Colors
swipeRefreshLayout.setColorSchemeColors({COLOR VALUE},{COLOR VALUE },
any number of colors values..., {COLOR VALUE});
  • Passing Colors Resources Ids
swipeRefreshLayout.setColorSchemeResources( R.color.{your Color resource Id},
any number of colors ids... , R.color.{your Color resource Id})```
-1

In my case the

setColorSchemeColors(R.color.primary), directly adding the color(Color.parse("#ffbea12")) instead of resource reference should work but I had to use the resources

and

setColorSchemeResources(R.color.primary) Both did not work as expected

Then I found that in colors.xml I had missing

 <color name="colorPrimary">@color/primary</color> 

So after adding it like this I GOT the desired color

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.