Android Rating Bar (filling the next star)

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 side of the star - it is selected, but when I click the right side - the next star is selected. I need a star that I click on so that it can be selected depending on which part of it I click. Please help me understand what is wrong.

+5
source share
1 answer

Perhaps this is an unfortunate side effect of a RatingBar that extends the ProgressBar , which internally uses int progress with rounding (rather than ceil ) to go from the search position float to int progress. The best solution I've found is to set stepSize to 0.01 , not 1 , and 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) ); } } });

+7
source

Source: https://habr.com/ru/post/1236302/


All Articles