Android Ratingbar: Getprogressdrawable Doesn't Cast To Right Class
Solution 1:
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 :
LayerDrawablestars= 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()
}
}
Solution 2:
LayerDrawable is used in API v21.
Solution 3:
Creating a subclass of RatingDialog and using that instead ofandroid.widget.RatingBar
worked for me. Here is the code I used.
Subclass:
publicclassRatingBarextendsandroid.widget.RatingBar {
publicRatingBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
publicRatingBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
Then in my layouts:
<your.package.RatingBar
android:id="@+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
Then in the classes where you are casting the class, just use your.package.RatingBar
instead of android.widget.RatingBar
Solution 4:
Came accross the same issue today when creating a RatingBar programmatically. The following workaround is working for me also on Android 4.x devices using the AppCompatRatingBar:
AppCompatRatingBarrb=newAppCompatRatingBar(activity);
LinearLayout.LayoutParamslayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
rb.setLayoutParams(layoutParams);
rb.setStepSize(1.0f);
rb.setMax(5);
LayerDrawablelayerDrawable=null;
if (rb.getProgressDrawable() instanceof LayerDrawable) {
layerDrawable = (LayerDrawable) rb.getProgressDrawable();
} elseif (rb.getProgressDrawable() instanceof DrawableWrapper) {
DrawableWrapperwrapper= (DrawableWrapper) rb.getProgressDrawable();
if (wrapper.getWrappedDrawable() instanceof LayerDrawable) {
layerDrawable = (LayerDrawable) wrapper.getWrappedDrawable();
}
}
if (layerDrawable != null) {
layerDrawable.getDrawable(2).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
layerDrawable.getDrawable(0).setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
layerDrawable.getDrawable(1).setColorFilter(ContextCompat.getColor(activity, R.color.primary), PorterDuff.Mode.SRC_ATOP);
}
Solution 5:
Please try this trick, It will work fine by adding getCurrent() method to return a drawable.
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable().getCurrent();
stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP);
Post a Comment for "Android Ratingbar: Getprogressdrawable Doesn't Cast To Right Class"