I have a custom RatingBar in my app that should display either the site average rating or the user rating for a movie if a user has rated it. The color should change depending on whether the rating displayed is the site average or a user rating.
Here’s the process:
User views movie. Site average rating for the movie is displayed.
Layout code for the RatingBar:
<RatingBar android:id="@+id/ratingBar" android:isIndicator="true"
android:layout_margin="10dip" style="@style/RatingBar"
android:progressDrawable="@drawable/site_rating_bar" android:stepSize="0.1" />
RatingBar style:
<style name="RatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:numStars">10</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minHeight">30dip</item>
<item name="android:maxHeight">30dip</item>
</style>
site_rating_bar.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background"
android:drawable="@drawable/btn_rating_star_off" />
<item android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/btn_rating_star_off" />
<item android:id="@+android:id/progress"
android:drawable="@drawable/btn_site_rating_star_on" />
</layer-list>
The user can click on the RatingBar and bring up a dialog where they can add their own custom rating of the movie. Upon close of the dialog, the code updates the rating displayed in the RatingBar above. I’d like to also change the ProgressDrawable so that the color of the stars changes to indicate the user has added a custom rating.
But when I try to set the ProgressDrawable programmatically it does update it so it’s displaying the right color, but it only displays a single star, stretched across the screen.
ratingBar.setProgressDrawable(thisActivity.getResources().getDrawable(R.drawable.user_rating_bar));
user_rating_bar.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background"
android:drawable="@drawable/btn_rating_star_off" />
<item android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/btn_rating_star_off" />
<item android:id="@+android:id/progress"
android:drawable="@drawable/btn_user_rating_star_on" />
</layer-list>
I’ve tried saving the original bounds and resetting them, but it didn’t help:
Rect bounds = ratingBar.getProgressDrawable().getBounds();
ratingBar.setProgressDrawable(thisActivity.getResources().getDrawable(R.drawable.user_rating_bar));
ratingBar.getProgressDrawable().setBounds(bounds);
Also tried resetting the numStars in case it had gotten lost.
Any ideas greatly appreciated. TIA. 🙂
I’m going through this same thing right now…I can’t for the life of me get it to work as expected through code, so I’m going the janky-layout route. 🙁
Then just flipping the visibility on them when needed:
Sucks, but it works. Let me know if you come up with any other solves.