I have the following RatingBar:
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:numStars="5"
android:max="5"
android:stepSize="1.0"
android:layout_marginTop="12dp"/>
When I press the left part of a star - it is selected, but when I press the right part - the following star is selected. I need the star I click on to be selected whichever part of it I click. Help me please to understand what is wrong.
It is perhaps an unfortunate side effect of RatingBar
extending ProgressBar
, which internally uses a int
progress with rounding (rather than ceil
) to go from a float
seek position to a int
progress. The best solution I have found is to set your stepSize
to 0.01
rather then 1
, then set
rb.setOnRatingBarChangeListener( new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged( final RatingBar ratingBar, final float rating, final boolean fromUser ) {
if ( fromUser ) {
ratingBar.setRating( Math.ceil(rating) );
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With