60

I want to change color of rating bar to golden.
I dont want to customize the stars, i just want to change the color for API 16 or above
I have tried following solutions but none of them worked out for me

1.    

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

2.

android:progressDrawable="@color/golden" in XML
ojas
  • 1,880
  • 5
  • 17
  • 33

14 Answers14

140

This option is now included in the AppCompat library. The AppCompat version of the RatingBar is used automatically.

http://developer.android.com/reference/android/support/v7/widget/AppCompatRatingBar.html

Example (from my own app):

<RatingBar
    android:id="@+id/rating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1"
    android:theme="@style/RatingBar"/>

With theme:

<style name="RatingBar" parent="Theme.AppCompat">
    <item name="colorControlNormal">@color/duskYellow</item>
    <item name="colorControlActivated">@color/lightGrey</item>
</style>
Ali Turki
  • 540
  • 2
  • 7
  • 16
theblitz
  • 6,161
  • 14
  • 52
  • 107
  • 4
    Along with the above answer, use style="@style/Base.Widget.AppCompat.RatingBar.Small" or @style/Base.Widget.AppCompat.RatingBar.Indicator in the rating bar widget to play around with the colour of the rating star and the size as well. – sissyphus_69 Apr 25 '16 at 17:47
  • 1
    is there any way to do this programmatically? i need to change star colour at runtime – Євген Гарастович Jan 10 '17 at 12:31
  • 3
    nevermind. found this awesome lib here: https://github.com/FlyingPumba/SimpleRatingBar we should give this man a medal. customizability is over the rooftop – Євген Гарастович Jan 11 '17 at 09:25
  • @Malder you have to remove `android:` from `colorControlActivated`. since i used auto complete code, `android:` prefix was added. without that prefix it works for KitKat as well – M.kazem Akhgary Oct 25 '18 at 15:39
  • there are 3 colors for rating bar, the one you missed is when 0 star selected. I don't know the key for that yet – Dr. aNdRO Nov 02 '18 at 10:46
41

On API 21 and higher, you can change the color of the filled stars with

     android:progressTint="@color/color"

and the color of the stroke with

     android:progressBackgroundTint="@color/color"
Vince
  • 1,464
  • 2
  • 23
  • 44
Raseem Ayatt
  • 730
  • 9
  • 14
14

As you mentioned that you want to change the star color without using the drawable. You can do it by few lines of code as below.

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#0064A8"),PorterDuff.Mode.SRC_ATOP);
Ralf
  • 14,050
  • 9
  • 45
  • 58
Chandra Sharma
  • 1,269
  • 12
  • 25
11

My task was to change the color of Rating Bar to the app's primary color for Android >=14 SDK. Only changes in code and in xml file helped me.

 mRatingBar = (RatingBar)view.findViewById(R.id.ratingBar);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        try {
            Drawable progressDrawable = mRatingBar.getProgressDrawable();
            if (progressDrawable != null) {
                DrawableCompat.setTint(progressDrawable, ContextCompat.getColor(getContext(), R.color.colorPrimary));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And in xml file

        <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:backgroundTint="@color/colorPrimary"
            android:numStars="5"
            android:progressTint="@color/colorPrimary"
            android:rating="0"
            android:secondaryProgressTint="@android:color/transparent"
            android:stepSize="1" />
Kiryl Belasheuski
  • 2,483
  • 2
  • 23
  • 38
7

My use case was to show different colors for different ratings. For example :

1- Red
2- Orange
3- Yellow
4- Light Green
5- Dark Green


Solution :

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

  @Override
  public void onRatingChanged(final RatingBar ratingBar, final float rating, final boolean fromUser) {
      setCurrentRating(rating);
    }
});

Then in your setCurrentRating() method :

 private void setCurrentRating(float rating) {
    LayerDrawable drawable = (LayerDrawable)rateOverall.getProgressDrawable();
    if(mContext!=null) {
          switch (Math.round(rating)) {
            case 1:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_red));
              break;
            case 2:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_orange));
              break;
            case 3:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_yellow));
              break;
            case 4:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_green_review));
              break;
            case 5:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_green));
              break;
          }
      setRatingStarColor(drawable.getDrawable(1), ContextCompat.getColor(mContext, R.color.transparent));
      setRatingStarColor(drawable.getDrawable(0), ContextCompat.getColor(mContext, R.color.light_grey_payment));

    }
  }

Then in your setRatingStarColor() method:

   private void setRatingStarColor(Drawable drawable, @ColorInt int color)
  {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
      DrawableCompat.setTint(drawable, color);
    }
    else
    {
      drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }
  }

Thanks to this answer which helped me solve this. Hope it helps someone !

Shubham Srivastava
  • 1,922
  • 13
  • 22
4

From XML

android:progressTint="#ffff8800"

Programmatically:

Drawable drawableReview = bar.getProgressDrawable();
drawableReview.setColorFilter(Color.parseColor("#ffff8800"), 
PorterDuff.Mode.SRC_ATOP);
FZs
  • 11,931
  • 11
  • 28
  • 41
3

try this,

You do need 3 star images (red_star_full.png, red_star_half.png and red_star_empty.png) and one xml file. If you want to golden star then use golden star image. this is for red star.

you can put ratingbar_color.xml in res/drawable.

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background" android:drawable="@drawable/red_star_empty" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/red_star_half" />
    <item android:id="@android:id/progress" android:drawable="@drawable/red_star_full" />
</layer-list>

and in rating bar definition use following.

<RatingBar android:progressDrawable="@drawable/ratingbar_red/>
Ganpat Kaliya
  • 1,190
  • 2
  • 8
  • 16
2
RatingBar ratingreview; 
ratingreview=(RatingBar)v.findViewById(R.id.ratingreview); 
ratingreview.setRating(Float.parseFloat(objrating)); 
Drawable drawable = ratingreview.getProgressDrawable(); 
drawable.setColorFilter(Color.parseColor("#6A9A28"), PorterDuff.Mode.SRC_ATOP);
Satan Pandeya
  • 3,460
  • 4
  • 22
  • 47
Soumen Das
  • 1,084
  • 13
  • 11
2

Simple solution, use AppCompatRatingBar and use below code

ratingbar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.colorAccent)));
Kuldeep Sakhiya
  • 2,956
  • 1
  • 14
  • 16
1

The simplest solution I have found is changing the color definition for the color "colorAccent" in the color.xml. That's entire magic without further coding.

1

Rating bar color change and size change:

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    style = "?android:attr/ratingBarStyleIndicator"
    android:progressTint="#ffff8800"
    android:rating="3.5"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:theme="@style/RatingBar" />
Taslim Oseni
  • 4,610
  • 10
  • 34
  • 50
murugan mani
  • 295
  • 2
  • 5
1

If you are running on kitkat that showing by default blue star instead yellow and golden then add this line : style="@style/Base.Widget.AppCompat.RatingBar.Small"

0

Use this code to get over the line

Drawable drawableReview = writeReviewRatingBar.getProgressDrawable();
        drawableReview.setColorFilter(Color.parseColor("#FFFDD433"), PorterDuff.Mode.SRC_ATOP);
0

Other solution using Kotlin Extensions that allows setup a different color by rating range:

Extension functions and property:

var RatingBar.colorScheme: Map<Float, Int>?
    get() = getTag(R.id.rating_bar_color_scheme) as Map<Float, Int>?
    set(value) = setTag(R.id.rating_bar_color_scheme, value)

fun RatingBar.setColorByRating(rating: Float) {
    val colorScheme = getTag(R.id.rating_bar_color_scheme) as Map<Float, Int>
    colorScheme[rating]?.also { color -> setLayerColor(2, color) }
    setLayerColor(1, ContextCompat.getColor(context, android.R.color.transparent))
    setLayerColor(0, ContextCompat.getColor(context, android.R.color.darker_gray))
}

fun RatingBar.setLayerColor(layerIndex: Int, color: Int) {
    val drawable = progressDrawable as LayerDrawable
    drawable.getDrawable(layerIndex).also {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            DrawableCompat.setTint(it, color)
        } else {
            it.setColorFilter(color, PorterDuff.Mode.SRC_IN)
        }
    }
}

For use it:

val red = ContextCompat.getColor(requireContext(), R.color.redColor)
val yellow = ContextCompat.getColor(requireContext(), R.color.yellowColor)
val green = ContextCompat.getColor(requireContext(), R.color.greenLightColor)

rbStars?.colorScheme = mapOf(
        1.0F to red,
        2.0F to red,
        3.0F to yellow,
        4.0F to green,
        5.0F to green
)

rbStars?.setColorByRating(rating)
carboleda
  • 41
  • 1
  • 4