I am an android beginner and I want to make my custom ratingBar.
Disclaimer: it's not a duplicate. because all the posts I have read asks about how to change colors of the star and how to remove the stroke. That is NOT what I want. I want to have the stroke and to be able to change the color of the border.
With a transparent and yellow stroke while empty, and yellow background and stroke for half and filled.
I do NOT want to use pngs. I have my own already but they are too small. I just dont want to ask the designer to make new ones if I can make the stars using only XML attributes and drawables.
<RatingBar android:id="@+id/thread_rating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:isIndicator="true" android:numStars="5" android:progressBackgroundTint="@color/gray" android:progressTint="@color/gold" android:rating="2.5" android:secondaryProgressTint="@color/gray" android:stepSize="0.5" style="?android:attr/ratingBarStyleSmall" />
I work on this with another engineer. He also wrote code in a java file
private void setRatingBarStart(RatingBar rating_bar) { LayerDrawable stars = (LayerDrawable) rating_bar.getProgressDrawable(); stars.getDrawable(2) .setColorFilter(getResources().getColor(R.color.gold), PorterDuff.Mode.SRC_ATOP); // for filled stars stars.getDrawable(1) .setColorFilter(getResources().getColor(R.color.light_gray), PorterDuff.Mode.SRC_ATOP); // for half filled stars stars.getDrawable(0) .setColorFilter(getResources().getColor(R.color.light_gray), PorterDuff.Mode.SRC_ATOP); // for empty stars
[So now it has a grey background, yellow for filled, and NO stroke now.
And I want it to have transparent background, yellow stroke and yellow for filled. Looks like this
I already know how to set the background transparent and to make it yellow. I just dont know how to set the stroke color. Thanks a lot!!!!
If you want to golden star then use golden star image. this is for red star. you can put ratingbar_color. xml in res/drawable.
Custom Rating Bar in AndroidCopy your images in the drawable folder and remember image size should be according to the size you want. Step 2. Make XML for the rating bar selector in the drawable folder like below. We made two files, one for the fill or highlighted part and one for the empty or un-highlighted part.
Put all drawable color to Yellow(in your case getResources().getColor(R.color.gold)
for all) for set strokes and background. (for background,secondary progress and progress)
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); stars.getDrawable(0).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); stars.getDrawable(1).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
remove progressBackgroundTint
,progressTint
,secondaryProgressTint
attributes from RatingBar
no need of it.
<RatingBar android:id="@+id/ratingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:rating="2.5"/>
and result is ::
Hope this will help (pink color is just background color)
after set drawable 1 and 0 to red:
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); stars.getDrawable(0).setColorFilter(Color.Red, PorterDuff.Mode.SRC_ATOP); stars.getDrawable(1).setColorFilter(Color.Red, PorterDuff.Mode.SRC_ATOP);
Don't use any library or style for that. Use bellow step you get the result.
create a file in drawable folder as custom_ratingbar.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" /> <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/star_empty" /> <item android:id="@android:id/progress" android:drawable="@drawable/star_full" /> </layer-list>
then use following in your RatingBar :
<RatingBar android:id="@+id/ratingBarChef" style="?android:attr/ratingBarStyleSmall" android:numStars="5" android:stepSize="0.2" android:rating="3.0" android:progressDrawable="@drawable/custom_ratingbar" android:isIndicator="false" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With