10

How do the following?

if RatingBar has 1-3 stars - red stars. if RatingBar has 4 stars - yellow stars. if RatingBar has 5 stars - green stars.

((RatingBar) layout.findViewById(R.id.ratingBar)).setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate()));


<RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ratingBar" android:stepSize="1" android:clickable="false"
            style="?android:attr/ratingBarStyleSmall" android:layout_gravity="right"
        android:layout_marginRight="15dp" />

EDIT:

 @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()));
        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;
    }

working code after answering @Murtaza Hussain:

RatingBar ratingBar = ((RatingBar) layout.findViewById(R.id.ratingBar));
        ratingBar.setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate()));
        LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
        if (ratingBar.getRating()<=3) {
            stars.getDrawable(2).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
        }
        if(ratingBar.getRating()==4){
            stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
        }
        if(ratingBar.getRating()==5){
            stars.getDrawable(2).setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);
        }
Valera Valerianov
  • 247
  • 1
  • 3
  • 14
  • Have a look at this post: http://stackoverflow.com/questions/2446270/android-ratingbar-change-star-colors – NightFury Dec 16 '14 at 12:16

2 Answers2

19
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;
 }
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
  • I do not need to change their meaning. It is set once and for all. I only need to display different stars. what you wrote - 3 stars if they are yellow. the other gray. if 2 stars - they are yellow - the others are gray. if the 5 stars - they are all yellow. I need anyway. if 1 or 2 or 3 stars - they are red, and the rest - gray. if 4 stars - 4 stars is yellow and one gray.if the 5 stars - all 5 - green – Valera Valerianov Dec 16 '14 at 11:56
  • didnt get you. what do you want ? – Murtaza Khursheed Hussain Dec 16 '14 at 11:57
  • if 1 or 2 or 3 stars - they are red, and the rest - gray. if 4 stars - 4 stars is yellow and one gray.if the 5 stars - all 5 - green – Valera Valerianov Dec 16 '14 at 11:58
  • empty star always gray! full of stars - change color depending on how many – Valera Valerianov Dec 16 '14 at 12:01
  • setOnRatingBarChangeListener fires when you change the rating. "rating" values indicates values in total, so you can change color in "setOnRatingBarChangeListener " – Murtaza Khursheed Hussain Dec 16 '14 at 12:01
  • You can read more here http://developer.android.com/reference/android/widget/RatingBar.OnRatingBarChangeListener.html about "setOnRatingBarChangeListener " – Murtaza Khursheed Hussain Dec 16 '14 at 12:02
  • say it again. I do not change the rating! I have a list of several RatingBar and when I load it, it once data is loaded. Edit to change them and they can not be – Valera Valerianov Dec 16 '14 at 12:04
  • Correctly. Edit your answer please. 1. My type int. if (ratingBar.getRating()<=3) { 2. extra code below – Valera Valerianov Dec 17 '14 at 03:46
  • @Murtaza Khursheed Hussain what if I want R.color.starFullySelected and R.color.starPartiallySelected to be exactly the same colour? How do I reflect that? – iOSAndroidWindowsMobileAppsDev Oct 12 '16 at 10:59
0
 android:progressBackgroundTint="#FFF"
    android:progressTint="@color/golden"

keep those both lines in RatingBar, active stars color will be golden. working for lollipop