I am using
Android RatingBar change star colors
This line of code
LayerDrawable stars = (LayerDrawable) rating_bar.getProgressDrawable();
works fine on my Nexus 4 emulator API v21. When trying the same code on my Galaxy S4 API v19, I get the following error message:
Caused by: java.lang.ClassCastException: android.support.v7.internal.widget.TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable
What is happening?
I am building with the latest build tools 22, my target API is 22 is as well.
What happens is clearly explained in the error message :
TintDrawableWrapper cannot be cast to android.graphics.drawable.LayerDrawable
The support lib creates a wrapper in order to handle retro compatibility, you just need to take it into account. Have a look at the lib implementation, it probably wraps around a LayerDrawable
.
You will need to do something along the lines of :
LayerDrawable stars = getProgressDrawable(rating_bar);
}
private LayerDrawable getProgressDrawable(RatingBar ratingBar) {
if (Build.VERSION >= LOLLIPOP) {
return (LayerDrawable) ratingBar.getProgressDrawable();
} else {
// fetch the LayerDrawable based on Google's support implementation,
// probably a simple findViewById()
}
}