Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 279
Gauthier Avatar asked Nov 18 '15 14:11

Gauthier


People also ask

How to use SwipeRefreshLayout android?

Add the SwipeRefreshLayout Widget To add the swipe to refresh widget to an existing app, add SwipeRefreshLayout as the parent of a single ListView or GridView . Remember that SwipeRefreshLayout only supports a single ListView or GridView child. You can also use the SwipeRefreshLayout widget with a ListFragment .

What is SwipeRefreshLayout?

Android SwipeRefreshLayout is a ViewGroup that can hold only one scrollable child. It can be either a ScrollView, ListView or RecyclerView.

How do I stop swipe refresh layout?

To disable the gesture and progress animation, call setEnabled(false) on the view. This layout should be made the parent of the view that will be refreshed as a result of the gesture and can only support one direct child.

How do you refresh on Kotlin?

Activity Code for Pull to Refresh Functionality in KotlininiRefreshListener() method will set a listener to the SwipeRefreshLayout and pull to refresh operation is performed by the user. In the above code, we are calling the function iniRefreshListener() and to load Xml we have to call the setContentView(R. layout.


2 Answers

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

like image 131
TrevJonez Avatar answered Sep 28 '22 18:09

TrevJonez


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));
}

}

like image 45
Jonas Avatar answered Sep 28 '22 20:09

Jonas