Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I use custom stars for Android RatingBar, always show half star for decimal values below 0.5

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 StarHalf StarFull 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

like image 302
Graham Baitson Avatar asked Jan 14 '14 12:01

Graham Baitson


1 Answers

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>
like image 122
ruslanys Avatar answered Nov 09 '22 08:11

ruslanys