Gauthier
Gauthier

Reputation: 4978

SwipeRefreshLayout App Theme

Is there a way to modify the color of the arrow in a SwipeRefreshLayout using theme ?

I know that you can use this code programaticaly

public void setColorSchemeResources (int... colorResIds)

But I'd like to have the arrow set to the theme of my app by default, and not having to change it in the code each time I use a SwipeRefrestLayout somewhere.

Upvotes: 7

Views: 3241

Answers (2)

Jonas
Jonas

Reputation: 2156

Best solution for me in this case was to extend SwipeRefreshLayout and set the colors there, then use this class in xml or code when needed.

public class ColoredSwipeRefreshLayout extends SwipeRefreshLayout {

public ColoredSwipeRefreshLayout(@NonNull Context context) {
    super(context);
    setColors();
}

public ColoredSwipeRefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    setColors();
}

private void setColors() {
    setColorSchemeColors(
            ContextCompat.getColor(getContext(), R.color.blue_main),
            ContextCompat.getColor(getContext(), R.color.blue_accent),
            ContextCompat.getColor(getContext(), R.color.blue_dark));
}

}

Upvotes: 5

TrevJonez
TrevJonez

Reputation: 959

As of support v4 23.0.1 the only attribute pulled from xml in the SwipeRefreshLayout constructor is android.R.attr.enabled

Meaning no, the only way to set the colors, is in code.

However you could create an array resource of the color id's to hold your color combo and reference that rather than having the duplicated list throughout your code base. Not much better but at least a change then only requires touching a single file.

Edit 1: The above is still true as of 24.2.1

Upvotes: 9

Related Questions