androidcolorfilterlayerdrawable

ProgressDrawable setColorFilter doesn't work in Nexus 5


I have a RatingBar :

<RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleX="0.75"
        android:isIndicator="false"
        android:scaleY="0.75"
        android:id="@+id/ratingBar"
        android:stepSize="0.5"
        android:numStars="5" />

and I'm using a color filter to make the rating bar stars pink in color like so:

    ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    Drawable progressDrawable = ratingBar.getProgressDrawable();
    if (progressDrawable instanceof  LayerDrawable) {
        LayerDrawable stars = (LayerDrawable) progressDrawable;
        stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.ColorSecondary), PorterDuff.Mode.SRC_ATOP);
    }

This works fine in all phones except the Nexus 5 ( Android version 6.0 ) where the 5 stars are all pink, but filled by default. Even when I click on the stars, they don't change color, all 5 remain filled.

However when I do ratingBar.getRating(), it returns the rating of where my user has touched the rating bar which means it's working, just the color filter is malfunctioning.

If I remove the color filter, the RatingBar works fine with the default colors.

Can't seem to find a solution to this anywhere. Thanks in advance.


Solution

  • I'm sorry that it's not the answer, but what's driving the requirement setting it from code ? And .setProgressDrawable should work if it's defined correctly

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>
    
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="5dip" />
                <gradient
                    android:startColor="@color/progress_start"
                    android:endColor="@color/progress_end"
                    android:angle="270" 
                />
            </shape>
        </clip>
    </item>
    
    </layer-list>
    

    And need more detail about progress bar's colors filters check this link : http://ambracode.com/index/show/14297