STARGATEBG STARGATEBG - 3 months ago 20
Android Question

How to make precise rating bar in Android?

I need to make precise rating bar with 5 stars, but when i have something like 2.7 rating i need to have 2.7 stars in white the others in grey. I was thinking something with the seeker bar. I added 5 stars and can easily make 4 grey when I have rating 1, but not sure how to make them partially colored.

enter image description here

I have something like this now :

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
setStars(i);
}

private void setStars(int stars){
switch (stars){
case 0:
star1.setVisibility(GONE);
star2.setVisibility(GONE);
star3.setVisibility(GONE);
star4.setVisibility(GONE);
star5.setVisibility(GONE);
break;
case 2:
star1.setVisibility(VISIBLE);
star2.setVisibility(GONE);
star3.setVisibility(GONE);
star4.setVisibility(GONE);
star5.setVisibility(GONE);
break;


And so on but I need it to be precise. I know i can probably make 10 states of the star for 1/10, 2/10 filled etc but that will be too much work.

Answer

Try this:

<RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="0.1"
        android:rating="2.7" />