-2

Using Android RatingBar, I want the ratings stars to have the following colors (all stars the same color per rating):

  • One star: red
  • Two stars: yellow
  • Three stars: blue
  • Four stars: green
  • Five stars: dark green

This is my code:

RatingBar rateDoctor;

rateDoctor = (RatingBar)findViewById(R.id.rateDoctor);

rateDoctor.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {


    }
});

And my XML:

<RatingBar android:id="@+id/rateDoctor" android:layout_marginTop="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="1.0" />
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Abhishek Kumar
  • 1,255
  • 13
  • 21

2 Answers2

0

try this

if(ratingbar.getRating()<=1)
{
LayerDrawable stars = (LayerDrawable) ratingBar
                .getProgressDrawable();
        stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.red),
                PorterDuff.Mode.SRC_ATOP); // for filled stars
        stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.hal_selected),
                PorterDuff.Mode.SRC_ATOP); // for half filled stars
        stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.no_selected),
                PorterDuff.Mode.SRC_ATOP);
}
Mehul Gajjar
  • 431
  • 5
  • 20
-1

References : How to create Custom Ratings bar in Android

 <RatingBar
    android:id="@+id/ratingBar2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:progressDrawable="@drawable/custom_ratingbar_selector"
    android:numStars="8"
    android:stepSize="0.2"
    android:rating="3.0" />