How can I set the star color of the ratingbar?
Solution 1
The simpliest way:
android:progressTint="@color/color"
Smooth and shiny.
Solution 2
This worked for me:
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#FFFDEC00"), PorterDuff.Mode.SRC_ATOP);
Solution 3
I've found that just setting the progressTint is not a complete solution. It will change the color of the star but not on partial star fills if your stepSize is less than 1. In my experience you also need to set the secondaryProgressTint in order to stop the default star tint from rearing its ugly head. Here is my code:
android:progressTint="@color/accent"
android:secondaryProgressTint="@android:color/transparent"
Hope this helps someone in my situation.
Solution 4
Have you tried an xml theme, such as
<?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/star_empty_show"/>
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_empty_show"/>
<item
android:id="@android:id/progress"
android:drawable="@drawable/star_filled_show"/>
</layer-list>
calling it in your xml as
<RatingBar
android:id="@id/rate"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5.0dip"
android:isIndicator="true"
android:max="5"
android:numStars="5"
android:progressDrawable="@drawable/ratebar_theme"
android:stepSize="0.1" />
and using your own drawables?
Solution 5
You do need 3 star images (star_filled_show.png, star_half_show.png and star_empty_show.png) and one xml, that's all.
Put these 3 images into res/drawable
.
Put there the following ratingbarstars.xml
:
<?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/star_empty_show"/>
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_half_show"/>
<item
android:id="@android:id/progress"
android:drawable="@drawable/star_filled_show"/>
</layer-list>
Tell your ratingbar definition to use this drawable
<RatingBar android:progressDrawable="@drawable/ratingbarstars.xml"/>
Related videos on Youtube
Finn Su
Updated on July 09, 2022Comments
-
Finn Su almost 2 years
How can I set the star color of the ratingbar? I want yellow stars.
-
Vikram almost 11 yearsTry this: Android RatingBar change star colors.
-
Vishal Kumar about 8 yearsFind the easiest way if you are using AppCompat activity stackoverflow.com/questions/2446270/…
-
-
Graham Baitson over 10 yearsHi kwishnu, I've tried this solution but when I try to set the rating to something like 1.1, 1.2, etc... it's display as 1.5. I have taken 3 star images, one empty, one half full and one full. I presume it's supposed to interpolate between them to get the fractional components, I'm just wondering has anyone else had the same issue or am I doing something noticeably wrong?
-
Jayshil Dave over 8 yearsi like this answer in this you wont need custom stars. I just need to change the color as the question not stars. The only drawback is a single color i guess one would need to modify the other drawables as well
-
MiguelCatalan about 8 years@suku yup, but the question didn't mention any restrictions regarding of minSDK.
-
chubao almost 8 yearsYou have to set
android:progressBackgroundTint
as well for the empty star. -
ViliusK almost 8 yearsAttributes
progressTint
andsecondaryProgressTint
are only used in API level 21 and higher. -
Pallavi over 7 years@ViliusK what will be the best way to make this work for all api versions of android
-
iOSAndroidWindowsMobileAppsDev over 7 yearsHow about when the star is half-filled? I want the star to be half yellow and half grey - or something
-
Anand Savjani over 7 yearsI have no much idea about it but you can do it using gradient or porterduff mode for that