0

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>
3
  • " 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." ======> This means you may not be correctly updating the rating bar. So you just check whether the rating bar rating is correctly changed at your required area of the code!
    – sai
    Jan 2, 2014 at 6:24
  • Shouldn't IDs start with '@android:id' instead of '@+id' in ratingstars.xml?
    – molnarm
    Jan 5, 2014 at 14:17
  • No, since it is the first occurence in the xml file of the ids
    – Gooey
    Jan 5, 2014 at 15:31

2 Answers 2

3

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>
0
1

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

6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.