Search code examples
androidratingbar

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


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"
      />

Solution

  • You can effectively backport the fix by doing:

    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N) { 
      try { 
        Field field = 
    AbsSeekBar.class.getDeclaredField("mTouchProgressOffset"); 
        field.setAccessible(true); 
        field.set(mRatingBar, 0.6f); 
      } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace(); 
      } 
    }
    

    See the original commit that fixed it.