Ratingbar Android - Custom Draw Runtime
I have ratingbar shown in a list. As people rate the items from 1 to 4 stars - the ratingbar should change either color of the stars or the background or overlay or something to sy
Solution 1:
I suppose you're looking for color tint:
ratingBar.setOnRatingBarChangeListener(newRatingBar.OnRatingBarChangeListener() {
@OverridepublicvoidonRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
finalLayerDrawablelayerDrawable= (LayerDrawable) ratingBar.getProgressDrawable();
int color;
switch ((int) rating) {
case1:
color = Color.RED;
break;
case2:
case3:
color = Color.YELLOW;
break;
case4:
default:
color = Color.GREEN;
break;
}
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), color);
}
});
It works not very smooth, but it should be a point to start.
Also, you can set the color tint in touch listener - that way will be better for you, I guess:
ratingBar.setOnTouchListener(newView.OnTouchListener() {
privateintlastColoredProgress=0;
@OverridepublicbooleanonTouch(View v, MotionEvent event) {
intprogress= ratingBar.getProgress();
if (progress != lastColoredProgress) {
lastColoredProgress = progress;
finalLayerDrawablelayerDrawable= (LayerDrawable) ratingBar.getProgressDrawable();
int color;
switch (lastColoredProgress) {
case0:
case1:
color = Color.RED;
break;
case2:
case3:
color = Color.YELLOW;
break;
case4:
default:
color = Color.GREEN;
break;
}
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), color);
}
returnfalse;
}
});
Post a Comment for "Ratingbar Android - Custom Draw Runtime"