My use case was to show different colors for different ratings. For example :
1- Red
2- Orange
3- Yellow
4- Light Green
5- Dark Green
Solution :
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(final RatingBar ratingBar, final float rating, final boolean fromUser) {
setCurrentRating(rating);
}
});
Then in your setCurrentRating()
method :
private void setCurrentRating(float rating) {
LayerDrawable drawable = (LayerDrawable)rateOverall.getProgressDrawable();
if(mContext!=null) {
switch (Math.round(rating)) {
case 1:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_red));
break;
case 2:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_orange));
break;
case 3:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_yellow));
break;
case 4:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_green_review));
break;
case 5:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_green));
break;
}
setRatingStarColor(drawable.getDrawable(1), ContextCompat.getColor(mContext, R.color.transparent));
setRatingStarColor(drawable.getDrawable(0), ContextCompat.getColor(mContext, R.color.light_grey_payment));
}
}
Then in your setRatingStarColor()
method:
private void setRatingStarColor(Drawable drawable, @ColorInt int color)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
DrawableCompat.setTint(drawable, color);
}
else
{
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}
Thanks to this answer which helped me solve this.
Hope it helps someone !