15

I've looked up a number of posts (e.g. Android RatingBar change star colors, Change the color of the stars in the rating bar where the rating bar is created dynamically in android, How can I set the star color of the ratingbar?) in order to change the colours of the stars in the RatingBar. I followed the posts and was able to change the stars for the custom RatingBar, but in doing this I was no longer able to set the value to be a decimal lower than a half (e.g. 0.2). It would always show up as the half (e.g. 0.5). When I set the value to be above the half (e.g. 0.7) it displays perfectly.

The following are the sample images that I am using for my custom RatingBar (i.e. ratingbar_empty.png, ratingbar_half.png, ratingbar_full.png)

Empty Star Half Star Full Star

The following is the xml to use the custom stars (ratingbar_custom.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/ratingbar_empty" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/ratingbar_half" />
    <item android:id="@android:id/progress" android:drawable="@drawable/ratingbar_full" />
</layer-list>'

The following is then how I use my custom RatingBar in my layout.

<RatingBar 
    android:id="@+id/rb_rating"
    android:numStars="7"
    android:rating="0"      
    android:stepSize="0.01"                     
    android:isIndicator="true"                                                            
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:progressDrawable="@drawable/ratingbar_custom" />

When I then put it all together and set the value of the RatingBar to be 0.2, it sets the RatingBar visually to 0.5, as shown below. Has anyone any ideas why this is happening or what I'm doing wrong? Any help would be greatly appreciated. Thanks

Setting custom RatingBar to 0.2, but visually showing 0.5

Community
  • 1
  • 1
Graham Baitson
  • 580
  • 1
  • 11
  • 29
  • Did you tried using a bigger step size, like `0.1`? – user Jan 14 '14 at 16:02
  • Hi @Luksprog, thanks for your comment. Yes I've tried that aswell and it's still the same. See it's pretty strange that it's only happening when I set the value below the half, when the set the value above the half it works fine? – Graham Baitson Jan 14 '14 at 16:20
  • Follow this to set custom color to default ratingbar https://stackoverflow.com/questions/2446270/android-ratingbar-change-star-colors/29853214#29853214 – Arshad Dec 09 '15 at 15:40

1 Answers1

31

You don't need star_half.

Try to do like this:

<?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/star_none" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/star_none" />
    <item android:id="@android:id/progress" android:drawable="@drawable/star_fill" />
</layer-list>
ruslanys
  • 1,183
  • 2
  • 13
  • 25