2

[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?

Vamsi Challa
  • 11,038
  • 31
  • 99
  • 149

3 Answers3

1

Working in lollipop set all states Empty star Half star Full star on rating change.

LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), Color.WHITE);   // Empty star
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), Color.parseColor("#757675")); // Partial st
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), Color.parseColor("#757675"));  // Full

Update Working Demo:

MainActivity

package pk.sohail.gallerytest.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.widget.RatingBar;

import pk.sohail.gallerytest.R;

public class MainActivity extends Activity {

    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

                LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
                DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), Color.parseColor("#3C3F41"));
                DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), Color.parseColor("#F7C065"));
                DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), Color.parseColor("#F7C065"));
            }
        });
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white">


    <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_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal"
        android:isIndicator="false"
        android:numStars="5"
        android:stepSize="1" />
</RelativeLayout>

app gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "pk.sohail.gallerytest"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'

}

enter image description hereenter image description here enter image description hereenter image description here

Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
1

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)

Sujay
  • 3,417
  • 1
  • 23
  • 25
0

Strangely Lolipop have this problem, not sure why! I got rid of this problem by using this and it works with Lolipop and all pre and post Lolipop.

Create a ColorState using the color you want to use for RatingBar

final ColorStateList colorStateList = ColorStateList.valueOf(<int color value>)

Then use ViewCompat

ViewCompat.setBackgroundTintList(<Your RatingBar>,colorStateList)
Debashis
  • 898
  • 8
  • 9