Подтвердить что ты не робот

Как изменить цвет RatingBar в зависимости от количества звезд?

Как сделать следующее?

если RatingBar имеет 1-3 звезды - красные звезды. если RatingBar имеет 4 звезды - желтые звезды. если RatingBar имеет 5 звезд - зеленые звезды.

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

рабочий код после ответа @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);
        }
4b9b3361

Ответ 1

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);

ИЛИ

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);

Вы можете сделать это на

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {
            // This event is fired when rating is changed   
        }    
    }); 

Из вашего кода

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

Ответ 2

 android:progressBackgroundTint="#FFF"
    android:progressTint="@color/golden"

сохраните эти обе строки в RatingBar, цвет активных звезд будет золотым. работает на леденец

Ответ 3

Это мое решение, когда рейтинг меньше или равен 3, тогда цвет звезды изменится на красный, больше 3 и меньше 5 изменится на желтый и, наконец, изменится на зеленый, если он будет равен 5.

Ну, я решил, что вам нужно изменить progressTint в xml с помощью кода, поэтому я обнаружил, что progressTintList справляется с задачей с помощью ColorStateList.

ratingBar.setOnRatingBarChangeListener(object: RatingBar.OnRatingBarChangeListener{
        override fun onRatingChanged(ratingBar: RatingBar?, rating: Float, fromUser: Boolean) {
            ratingBar?.numStars = 5
            if (rating <= 3){
                ratingBar?.progressTintList = ColorStateList.valueOf(Color.RED)
            }else if (rating > 3 && rating < 5){
                ratingBar?.progressTintList = ColorStateList.valueOf(Color.YELLOW)
            }else{
                ratingBar?.progressTintList = ColorStateList.valueOf(Color.GREEN)
            }
        }

    })