1

I am using CaldroidFragment for my application. Now I am trying to set the background color of a specific date using

CaldroidFragment.setBackgroundResourceForDate(int backgroundRes,
                                     java.util.Date date);

function. Now if I pass resource from xml, like R.color.blue as first parameter then it works, but I have to pass dynamic color value as background in runtime. I generate color dynamically within a range (something like interpolation). Now I am trying getResources().getColor() function to convert my color to resource.

private static final int COLOR_END = Color.parseColor("#BD4141");
private static final int COLOR_START = Color.parseColor("#69A864");



mCaldroidFragment.setBackgroundResourceForDate(
                        getResources().getColor(getInterPolateColor(2)),
                        new Date());


private int getInterPolateColor(int value) {
            return Utility.interpolateColor(COLOR_START, COLOR_END, value / (float) 15);
}



public static int interpolateColor(final int startColor, final int endColor, final float proportion) {
            final float[] hsva = new float[3];
            final float[] hsvb = new float[3];
            Color.colorToHSV(startColor, hsva);
            Color.colorToHSV(endColor, hsvb);
            for (int i = 0; i < 3; i++) {
                hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
            }
            return Color.HSVToColor(hsvb);
}



private static float interpolate(final float a, final float b, final float proportion) {
            return (a + ((b - a) * proportion));
}

But it shows following error

 android.content.res.Resources$NotFoundException: Resource ID #0xff79ab60
            at android.content.res.Resources.getValue(Resources.java:1123)
            at android.content.res.Resources.getColor(Resources.java:805)

Now is it possible to create background resource from color value which will act like R.color.xxx, or any solution to solve my problem.

sabbir
  • 438
  • 3
  • 11

2 Answers2

2

After reading the source code i dont think that there is other option then changing the source code to support colors and not only resources/

EE66
  • 4,601
  • 1
  • 17
  • 20
2

As far as I know, there is no way to dynamically change R items (R.color, R.id, etc.) at runtime. These resources are compiled at build time from your xml (and other static) resources.

Also, according to the CaldroidFragment GitHub readme, it does indeed look like you're expected to define custom colors in xml:

To use these methods, you should define your colors in color.xml and background in drawable folder:

caldroidFragment.setBackgroundResourceForDate(R.color.blue, blueDate);
caldroidFragment.setBackgroundResourceForDate(R.color.green, greenDate);

So, the short answer seems to be, no, you can't set your colors dynamically.

My only suggestion is that, if you can figure out a way to override/overload setBackgroundResourceForDate(), it'd be best to pass in a drawable resource instead of color for the background. This way, you'll be able to dynamically adjust the drawable (color, image, shape, etc.) while still keeping the same R.id and name.

hungryghost
  • 9,463
  • 3
  • 23
  • 36