How can I set the star color of the ratingbar? I want yellow stars.
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RatingBar"/>
with theme in styles
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/gray</item>
<item name="colorControlActivated">@color/yellow</item>
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"/>
try this one
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW,PorterDuff.Mode.SRC_ATOP);
It's a little complicated at the mentioned blog, I've used a similar but simplier way. You do need 3 star images (red_star_full.png, red_star_half.png and red_star_empty.png) and one xml, that's all.
Put these 3 images at res/drawable.
Put there the following ratingbar_red.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/red_star_empty" />
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/red_star_half" />
<item android:id="@android:id/progress" android:drawable="@drawable/red_star_full" />
</layer-list>
and, finally, tell your ratingbar definition to use this, i.e.
<RatingBar android:progressDrawable="@drawable/ratingbar_red"/>
That's it.