I've got Android's SwipeRefreshLayout working and am trying to customize the colors across all the pull to refreshes throughout the app. In order to follow the DRY principle, I've tried moving the desired colors to array.xml as follows:
<resources>
<array name="swipeRefreshColors">
<item>@color/pink</item>
<item>@color/green</item>
</array>
</resources>
However, when I try and import them into the swipe to refresh:
swipeRefreshLayout.setColorSchemeResources(R.array.swipeRefreshColors);
I get a Resources$NotFoundException:
android.content.res.Resources$NotFoundException: Resource ID #0x7f060001
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.getColor(Resources.java:887)
at android.support.v4.widget.SwipeRefreshLayout.setColorSchemeResources(SwipeRefreshLayout.java:477)
I've tried a couple things such as subclassing the SwipeRefreshLayout code and hard coding the colors there, but it's definitely a hack. There's got to be a way to reference an array of colors from the Activity to customize it.
Any help would be greatly appreciated!
Turns out I was missing two key pieces.
Wrong Code:
swipeRefreshLayout.setColorSchemeResources(R.array.swipeRefreshColors);
Correct Code:
swipeRefreshLayout.setColorSchemeColors(getResources().getIntArray(R.array.swipeRefreshColors));
There were two things I was missing.
1) I needed to indicate that I was getting an IntArray
from my array.xml file. This is done through getResources().getIntArray(R.array.swipeRefreshColors)
.
The answer was deleted, but thanks to whoever suggested this before.
2) The key part that was wrong is that I had to use setColorSchemeColors
instead of setColorSchemeResources
. I guess at some point in the build process the references I had in Array were converted to explicit color values.
Hopefully this can help someone else!
来源:https://stackoverflow.com/questions/28118038/how-to-pass-int-array-of-color-resource-ids-from-array-xml-to-swiperefreshlayout