how to change the color RatingBar depending on the number of stars?
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
OR
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.starFullySelected), PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.starPartiallySelected), PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.starNotSelected), PorterDuff.Mode.SRC_ATOP);
You can do this on
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// This event is fired when rating is changed
}
});
From your code
@Override
public View getItemView(int section, int position, View convertView, ViewGroup parent) {
LinearLayout layout;
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = (LinearLayout) inflator.inflate(R.layout.item_survey, null);
} else {
layout = (LinearLayout) convertView;
}
((TextView) layout.findViewById(R.id.questionSurvey)).setText(surveyBeans.get(section).get(position).getQuestion());
RatingBar ratingBar = ((RatingBar) layout.findViewById(R.id.ratingBar));
ratingBar.setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate()));
if (ratingBar.getRating() == 2f) {
//change color of two stars
} else if (ratingBar.getRating() == 3f) {
//change color of three stars
}
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
((TextView) layout.findViewById(R.id.commentSurvey)).setText(surveyBeans.get(section).get(position).getComment());
return layout;
}