2

For filling the color in rating bar for decimal rating is working fine for API below 23 and not working in API 23.

Here is the code:

RatingBar ratingBar = (RatingBar) view.findViewById(R.id.ratingbar);
ratingBar.setNumStars(5);
ratingBar.setMax(5);
ratingBar.setStepSize(0.1f);
ratingBar.setRating(0.5);
Drawable ratingBarColor = ratingBar.getProgressDrawable();
DrawableCompat.setTint(ratingBarColor, ContextCompat.getColor(getActivity(), R.color.red));

which will fill the color for 0.5(half) star with the red color. But for android API 23(MarshMallow) whatever you set the rating it's filling all the star.Can I know what's the change happened in API 23 which is causing this issue or let me know the problem with this code.

Handset used for testing (6.0): HTC One M8, Moto X Play, Nexus 6. 5.1.1: Moto X(First Gen),Nexus 4,Moto G1,G2

Edit: I tested by removing setTint in API23 which is setting white color with decimal rating.So it could be issue with DrawableCompat for setting color for decimal rating star.

Madhukar Hebbar
  • 3,113
  • 5
  • 41
  • 69

2 Answers2

4

For API 23(MarshMallow), RatingBar does suffer the problem of all stars changed color (regardless it's 1 or 5 stars) which look like it's always 5 stars.

UPDATED ANSWER: The following code works

if (ratingBar.getProgressDrawable() instanceof LayerDrawable) {
    LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
    DrawableCompat.setTint(stars.getDrawable(2), color);
}
else {
    // for Android 4.3, ratingBar.getProgressDrawable()=DrawableWrapperHoneycomb
    DrawableCompat.setTint(ratingBar.getProgressDrawable(), color);
}

The following doesn't work for Mashmallow

Drawable ratingBarColor = ratingBar.getProgressDrawable();
DrawableCompat.setTint(ratingBarColor, ContextCompat.getColor(getActivity(), R.color.red));

In the layout file remember to use android.support.v7.widget.AppCompatRatingBar, not RatingBar.

Desmond Lua
  • 6,142
  • 4
  • 37
  • 50
2

you can make the custom rating bar

1 just make the xml file in your drawable folder and name it rating_bar.xml

<?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/rating_empty" />
        <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/rating_empty" />
        <item android:id="@android:id/progress" android:drawable="@drawable/rating_filled" />
    </layer-list>

2 make the another drawable xml file and name it rating_empty.xml

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_unrated_star" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_unrated_star" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_unrated_star" />

<item android:drawable="@drawable/ic_unrated_star" />

3 make anothe drawable xml file for filled star and name it rating_filled.xml

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_rated_star" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_rated_star" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_rated_star" />

<item android:drawable="@drawable/ic_rated_star" />

4 goto your values folder and open style.xml and add the style

<style name="customRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/rating_bar</item>
    </style>

5 just use the custom rating bar, which you have just created

    <RatingBar
    style="@style/customRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center"
    android:numStars="5" />

Note: ic_rated_star and ic_unrated_star are the completly filled star and empty star image , which you want to show in your rating bar.

Hope this worked for you... best of luck...

Kushal
  • 795
  • 1
  • 5
  • 23