androidandroid-studiogradlecolorfilter

Changing Color of Image programmatically Android


I know there are several answers out there, but I have tried them with no success. I have code that was previously working that changes the color of an icon when loaded using a predefined resource color from the color.xml file. For different Flavors I have different color.xml files. I know that the color.xml file is getting picked up and used as other colors are successfully being used. As I said, this was previously working out of Eclipse and using an Ant build. However I have upgraded a number of libraries, so I suspect something may have changed in one to cause this problem. Even stranger, it works when running out of Android Studio in debug, but does NOT when I build using Gradle.

Here is the code that was working previously ( suspect the setColorFilter is the culprit?):

ImageButton btnYes = new ImageButton(mContext);
btnYes.setPadding(0, 15, 15, 15);
btnYes.setTag(1);
Drawable yesDrawable = Utilities.getAndroidDrawable("form_ratingyes", mContext);
btnYes.setImageDrawable(yesDrawable);
btnYes.setFocusableInTouchMode(false);
btnYes.setBackgroundColor(Color.WHITE);
btnYes.setColorFilter(ContextCompat.getColor(mContext,R.color.rating_off));

The color.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Bullet Icon Color for (Forms, In Progress, Sync and Submitted Status)-->
    <color name="bullet">#0378AF</color>

    <!-- Navigation Bar Color-->
    <color name="navigation_bar">#F68A33</color>

    <!-- Menu Icon Color-->
    <color name="menu_tint">#F68A33</color> 
    <color name="form_icons">#F68A33</color>

    <!-- Rating Field Button Color -->
    <color name="rating_on">#F68A33</color>
    <color name="rating_off">#A2AAAD</color>

    <!-- Action Button Color -->
    <color name="login_button_default">#F68A33</color>
    <color name="login_button_selected">#B0540B</color>
</resources>

And the image looks like this:

Basically a white circle with a transparent "Y" and outer circle. The idea is that the white would be replaced by the color "rating_on".

enter image description here

When running correctly it should look like this:

enter image description here

If the suspect is the gradle file I can add it, but doesn't Android Studio use that to build the debug version too?

I have tried a number of different ways to load the icon color, all with no success. I also tried tacking .getDrawable() in front of the call to setColorFilter. Sadly it is hard to debug because in Android Studio it works - only when I generate the actual APK does it fail.


Solution

  • The answer is that the code above is fine. The problem was in the bowels of the Utilities.getAndroidDrawable("form_ratingyes", mContext); call it looked like this:

    public static Drawable getAndroidDrawable(String drawableName, Context context){
        int resourceId= context.getResources().getIdentifier(drawableName, "drawable", imagepackage);
        if(resourceId==0){
            return null;
        } else {
            return ContextCompat.getDrawable(context, resourceId);
        }
    

    The imagepackage was:

    public final static String imagePackage = "com.mycompany.myapp";
    

    So obvious, but I never delved into that method. Shame on me. Somehow in the old Ant build it was including all the images under the older package name (which was hard coded) - not the new package name. Yep fun one to debug (not my code so was unaware of what it was doing).

    Just needed to call for the package dynamically:

    public static Drawable getAndroidDrawable(String drawableName, Context context){
        int resourceId= context.getResources().getIdentifier(drawableName, "drawable", context.getPackageName());
        if(resourceId==0){
            return null;
        } else {
            return ContextCompat.getDrawable(context, resourceId);
        }