Android ratingbar doesn't update

1.8k Views Asked by At

I've made a ratingbar in my app which uses a custom style, using my own stars.

My ratingbar is defined as followed:

 <RatingBar
           android:layout_width="wrap_content"
           android:layout_height="15dp"
           android:id="@+id/ratingBar"
           android:numStars="10"
           android:stepSize="1"
           android:rating="10"
           android:layout_marginTop="5dp"
           android:layout_below="@id/feedbackTable"
           android:layout_toRightOf="@id/ratingText"
           android:layout_marginLeft="5dp"
           style="@style/starBar" />

The style that I use in styles.xml is:

<style name="starBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingstars</item>
        <item name="android:minHeight">22dip</item>
        <item name="android:maxHeight">22dip</item>
    </style>

With ratingstars.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/background" android:drawable="@drawable/ratingstars_full_empty" />
    <item android:id="@+id/secondaryProgress" android:drawable="@drawable/ratingstars_full_empty" />
    <item android:id="@+id/progress" android:drawable="@drawable/ratingstars_full_filled" />
</layer-list>

When I adjust the android:rating it shows the correct amount of stars filled and being empty in the preview. However, using my emulator or real device the bar stays completely filled with stars (10/10). I've tried adjusting it programmatically by using the following piece of code:

 ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Log.d("rating", rating+"");
                ratingbar.setRating(rating);
            }
        });

Now the log from logcat shows actually that rating IS changed, but the bar does not visually update itself with e.g. 6 stars being full, and 4 empty and I have no clue why.

The drawables are correct as the preview also shows it correct, so what can cause this?

Edit:

This is ratingstars_full_filled.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

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

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

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

</selector>

And here is ratingstars_full_empty.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

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

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

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

</selector>
2

There are 2 best solutions below

6
On

It looks like it's not a problem with the listener being called, but rather just the custom android:progressDrawable style attribute you're applying to it. My guess is that the drawable you're using is doing unexpected things. Try removing style="@style/starBar" from your RatingBar and see if things are working as expected. If so, that's your problem

0
On

You should change the id attribute in ratingstars.xml from "@+id/..." to "@android:id/...", since the resource is part of the android system, and already defined in the default android progress 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/ratingstars_full_empty" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/ratingstars_full_empty" />
    <item android:id="@android:id/progress" android:drawable="@drawable/ratingstars_full_filled" />
</layer-list>