Android: Change color of ratingbar to golden

AndroidRatingbar

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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/lightGrey</item>
    <item name="colorControlActivated">@color/duskYellow</item>
</style>

Solution 2 - Android

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"

Solution 3 - Android

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

Solution 4 - Android

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" />

Solution 5 - Android

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 !

Solution 6 - Android

From XML

android:progressTint="#ffff8800"

Programmatically:

Drawable drawableReview = bar.getProgressDrawable();
drawableReview.setColorFilter(Color.parseColor("#ffff8800"), 
PorterDuff.Mode.SRC_ATOP);

Solution 7 - Android

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/>

Solution 8 - Android

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

Solution 9 - Android

Simple solution, use AppCompatRatingBar and use below code

ratingbar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.colorAccent)));

Solution 10 - Android

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.

Solution 11 - Android

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" />

Solution 12 - Android

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"

Solution 13 - Android

Use this code to get over the line

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

Solution 14 - Android

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionojasView Question on Stackoverflow
Solution 1 - AndroidtheblitzView Answer on Stackoverflow
Solution 2 - AndroidRaseem AyattView Answer on Stackoverflow
Solution 3 - AndroidChandra SharmaView Answer on Stackoverflow
Solution 4 - AndroidKiryl BielašeŭskiView Answer on Stackoverflow
Solution 5 - AndroidShubhamView Answer on Stackoverflow
Solution 6 - AndroidParameshwar PanjaView Answer on Stackoverflow
Solution 7 - AndroidGanpat KaliyaView Answer on Stackoverflow
Solution 8 - AndroidSoumen DasView Answer on Stackoverflow
Solution 9 - AndroidKuldeep SakhiyaView Answer on Stackoverflow
Solution 10 - AndroidStanislav VincentView Answer on Stackoverflow
Solution 11 - Androidmurugan maniView Answer on Stackoverflow
Solution 12 - AndroidPradeep KumarView Answer on Stackoverflow
Solution 13 - AndroidMohammad Shahzeb KhanView Answer on Stackoverflow
Solution 14 - AndroidcarboledaView Answer on Stackoverflow