androidratingbar

RatingBar - Change stars color based on Rating - Issue with Lollipop


[EDIT 3] - Working Now Changed the code as below, on @sJy suggestion:

 LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); // Partial star
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary));
                    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        layerDrawable.getDrawable(1).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));
                        layerDrawable.getDrawable(2).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));
                        layerDrawable.getDrawable(0).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary));
                    } else {
                        layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar), PorterDuff.Mode.SRC_ATOP);
                    }

enter image description here


I am using AppCompatRatingBar to display user-selected rating. This code works fine in KitKat and Marshmallow, however the star's colour doesn't change in Lollipop.

Please see the images and code below:

Kitkat - 4 stars

enter image description here

Lollipop - clicked on two stars

enter image description here

Marshmallow - 3 stars

enter image description here

Code:

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {


                if ((int) rating == 1) {

                    ratingBar.setNumStars(5);
                    ratingBar.setProgress(1);
                    ratingBar.setRating(1);

                    LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); 
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));

                    }else {
                        layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar), PorterDuff.Mode.SRC_ATOP);
                    }

                    textViewRating.setText(getResources().getString(R.string.worst));

                } else if ((int) rating == 2) {

                    ratingBar.setNumStars(5);
                    ratingBar.setProgress(2);
                    ratingBar.setRating(2);

                    LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars)); 
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars));

                    }else {
                        layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars), PorterDuff.Mode.SRC_ATOP);
                    }

                    textViewRating.setText(getResources().getString(R.string.poor));
                } else if ((int) rating == 3) {

                    ratingBar.setNumStars(5);
                    ratingBar.setProgress(3);
                    ratingBar.setRating(3);

                    LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars)); 
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars));
                    }else {
                        layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars), PorterDuff.Mode.SRC_ATOP);
                    }

                    textViewRating.setText(Html.fromHtml(getResources().getString(R.string.onetime_watch_one_line)));
                } else if ((int) rating == 4) {

                    ratingBar.setNumStars(5);
                    ratingBar.setProgress(4);
                    ratingBar.setRating(4);

                    LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars)); 
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars));
                    }else {
                        layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars), PorterDuff.Mode.SRC_ATOP);
                    }

                    textViewRating.setText(getResources().getString(R.string.good));
                } else if ((int) rating == 5) {

                    ratingBar.setNumStars(5);
                    ratingBar.setProgress(5);
                    ratingBar.setRating(5);

                    LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars)); 
                        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars));
                    }else {
                        layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars), PorterDuff.Mode.SRC_ATOP);
                    }

                    textViewRating.setText(getResources().getString(R.string.excellent));
                }

            }

        });

[EDIT 1] As per @Sohail's suggestion, i added

DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary));

When clicked on the RatingBar, it disappears. For clarification, i added a background colour to the RatingBar.

Before

enter image description here

After

enter image description here

No resolution yet!!!

[EDIT 2] Adding AppCompatRatingBar xml, if it helps:

 <android.support.v7.widget.AppCompatRatingBar
                    android:id="@+id/ratingBar"
                    style="?android:attr/ratingBarStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="8dp"
                    android:background="@color/colorFiveStars"
                    android:isIndicator="false"
                    android:max="5"
                    android:numStars="5"
                    android:stepSize="1" />

Is the style by any change, source of the problem?


Solution

  • Try setting Tint directly using setTint()

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        layerDrawable.getDrawable(1).setTint(getResources().getColor(R.color.colorOneStar));
        layerDrawable.getDrawable(2).setTint(getResources().getColor(R.color.colorOneStar)); 
    }
    

    Note : getColor(int) is deprecated but it still works(Tested in Lollipop,Marshmallow)