1

I am trying to write a custom setter for SwipeRefreshLayout's

setColorScheme(int... colors).

But it seems that its parameter is varargs.

I can only bind a single color now like the following:

   @BindingAdapter("app:colorSchemeResources") 
    public static void bindRefreshColor(SwipeRefreshLayout swipeRefreshLayout,  int colorResId) {
            swipeRefreshLayout.setColorSchemeColors(colorResId);
    } 

xml:

  <android.support.v4.widget.SwipeRefreshLayout
                android:id="@+id/swipe_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:colorSchemeResources ="@{@color/primary}"
                />

My question is:

How can I write a custom setter for varargs?

How to bind varargs in the xml file?

2
  • please share some code what have you tried so far. xml file too. Commented Apr 29, 2016 at 3:20
  • sure, I have updated the question. I can only bind a single color by far. Commented Apr 29, 2016 at 3:39

1 Answer 1

1

Please try this:

Specify an integer array containing the colors you want in your colors.xml for example:

    <integer-array name="color_scheme" >
       <item>@color/first_color</item>
        <item>@color/second_color</item>
    </integer-array>

Change your bindingadapter like this:

    @BindingAdapter("app:colorSchemeResources")
    public static void bindRefreshColor(SwipeRefreshLayout swipeRefreshLayout, int[] colorResIds) {
    swipeRefreshLayout.setColorSchemeColors(colorResIds);
    }

And reference your array in your view:

        <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_layout"
        android:layout_width="match_parent"
        app:colorSchemeResources ="@{@intArray/color_scheme}"
        android:layout_height="wrap_content">
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it woks like a charm.The intArray is just what I want.
Where did you place integer-array name="color_scheme" ?
@kondal you can put it in values/colors etc xmls

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.