How can I set the color of android rating bar's stroke? (Not the color of the stars but the BORDER)

husky love picture husky love · Feb 13, 2016 · Viewed 14.7k times · Source

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.

enter image description here

And I want it to have transparent background, yellow stroke and yellow for filled. Looks like this enter image description here

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!!!!

Answer

KDeogharkar picture KDeogharkar · May 10, 2016

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 ::

image1

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);

image2