Android Nougat (7.0 / API 24) rating count is wrong in OnRatingBarChangeListener

Viewed 1326

I am using RatingBar in Android Nougat (7.0) And using OnRatingBarChangeListener to detect the rating changes.

It is giving wrong value of rating in callback method. So suppose if i click on 2nd star it gives me 3.0 and if i click 4th star it gives me count as 5.0 .

Another thing which i have noticed is count depends on where exactly i am clicking. If i click on first half part of star it returns proper count and if i click on second half of star it adds +1 to count.

Video link

ACTIVITY :

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar2);

    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
      @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

        Log.i(TAG, String.format("Rating is %f : fromUser %b", rating, fromUser));

        mTextView.setText(String.format("Rating is %f ", rating));
      }
    });

XML :

  <RatingBar
      android:id="@+id/ratingBar2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:isIndicator="false"
      android:stepSize="1"
      android:rating="5"
      />
3 Answers

I wrote a very simple workaround for this problem, on your rating bar, use step as "0.1" and on Your OnRatingBarChangeListener use this code snippet;

int rating = (int)Math.Ceiling(args.Rating);
if (rating != args.Rating)
{
    ratingBar.Rating = rating;
    return;
} 

// now the rating will be the tapped star
// you can do what you must do here

p.s. i wrote this in Xamarin.Android but I am pretty sure you can easily make it apply to java by changing the ceil function and using the setter for the rating of the RatingBar

Related