Simon
Simon

Reputation: 19938

Android: How do I change the rating an indicator rating bar?

I have an indicator rating bar:

<RatingBar
    android:id="@+id/ratingBar"
    style="?android:attr/ratingBarStyleIndicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:rating="2.5"
    android:numStars="5"/>

You can see that the xml tells the rating bar to reflect 2.5 stars currently.

At a click of a button, I want it to reflect 5 stars instead, so I wrote this code:

indicatorRatingBar = (RatingBar) findViewById(R.id.ratingBar);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        indicatorRatingBar.setIsIndicator(false);
        indicatorRatingBar.setRating(5);
        indicatorRatingBar.setIsIndicator(true);
    }
});

The rating bar does not update at all. I don't know if it is because it is an indicator rating bar?

How can I get this to work properly with the rating bar set at the indicator style?

Upvotes: 0

Views: 1830

Answers (2)

Simon
Simon

Reputation: 19938

I actually found the answer through trial and error. I just needed to call invalidate on the view as it has changed and it doesn't invalidate itself automatically.

indicatorRatingBar = (RatingBar) findViewById(R.id.ratingBar);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        indicatorRatingBar.setRating(5);
        indicatorRatingBar.invalidate();
        indicatorRatingBar.setIsIndicator(true);
    }
});

Upvotes: 2

Dhruvi
Dhruvi

Reputation: 1981

Change it to

indicatorRatingBar.setRating(5.0f);

Upvotes: 0

Related Questions