The call to getProgressDrawable () on a RatingBar displays only one star

I have a custom RatingBar in my application, which should display either the average site rating or the user rating for the movie, if the user rated it. The color should change depending on whether the average site rating or user rating is displayed.

Here's the process: The user is watching a movie. Displays the average site rating for the movie.

Layout code for 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" /> 

Rating 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 RatingBar and open a dialog in which they can add their own movie rating. At the end of the dialogue, the code updates the rating displayed in the RatingBar above. I would also like to change ProgressDrawable so that the color of the stars changes, indicating that the user has added a user rating.

But when I try to install ProgressDrawable programmatically, it updates it so that it displays the correct color, but it only displays one 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 tried to keep the original boundaries and reset them, but this did not help:

 Rect bounds = ratingBar.getProgressDrawable().getBounds(); ratingBar.setProgressDrawable(thisActivity.getResources().getDrawable(R.drawable.user_rating_bar)); ratingBar.getProgressDrawable().setBounds(bounds); 

Also tried resetting numStars in case it got lost.

Any ideas are greatly appreciated. TIA. :)

+4
source share
2 answers

I am experiencing the same thing right now ... I cannot get my life to work as expected using code, so I am following the janky-layout route .: (

 <RatingBar android:id="@+id/beer_rating" android:numStars="5" android:progressDrawable="@drawable/rating_bar" android:indeterminateDrawable="@drawable/rating_bar" android:stepSize="0.5" android:isIndicator="true" android:layout_gravity="center" style="?android:attr/ratingBarStyleSmall" /> <RatingBar android:id="@+id/beer_rating_average" android:indeterminateDrawable="@drawable/rating_bar_average" android:isIndicator="true" android:layout_gravity="center" android:numStars="5" android:progressDrawable="@drawable/rating_bar_average" android:stepSize="0.5" android:visibility="gone" style="?android:attr/ratingBarStyleSmall"/> 

Then simply flip through the visibility on them if necessary:

 findViewById(R.id.beer_rating).setVisibility(View.GONE); findViewById(R.di.beer_rating_average).setVisibility(View.VISIBLE); 

Sucks, but it works. Let me know if you come up with any other solutions.

+1
source

Try using: Use ProgressBar.setProgressDrawableTiled (Drawable)

and set minSDK to 21

0
source

All Articles