Search code examples
androidratingbar

Android RatingBar rating is not set correctly


I use two RatingBars in my xml layout:

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="0.25"
    android:rating="4.25"/>

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="0.25"
    android:rating="4.5"/>

Even though I set ratings to 4.25 and 4.5, both RatingBars display wrong number of stars filled. On an Android 6 device it looks like this:

ratingBars

On an android 4.3 device, it looks like this:

ratingBars

But on xml design view, I can see 4.25 and 4.5 are set. Can anyone tell me why? I do not do anything with the rating bars in my code).

Thanks.


Solution

  • In my opinion the problem is related to android:progressTint ,android:progressBackgroundTint & android:secondaryProgressTint. Try to add following lines to your rating bar:

    android:progressTint="Color1"
    android:progressBackgroundTint="Color2"
    android:secondaryProgressTint="Color3"
    

    also try to add style to your rating bar as following:

    <style name="RatingBar" parent="Theme.AppCompat"">
    <item name="colorControlNormal">@color/yourColor1</item>
    <item name="colorControlActivated">@color/yourColor2</item></style>
    

    and finally add a the following line in your rating bar

    style="@style/RatingBar"
    

    It will work for you.