Android – use clickable RatingBar in ListView

25. 04. 2018

Rating bar works well in ListView in Android 4 and higher. You can scroll the list by swiping along X axis and change the RatingBar value by swiping along Y axis.

The problems comes on Android 2.X, here all touch events are consumed by RatingBar and it is not possible to scroll your list by touching the RatingBar and drag.

You can fix it by simply adding this listener to your RatingBars:

listenerRatingBar = new OnTouchListener() {
  float xDown;
    
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
      // save down X coordinate
      xDown = event.getX();
    } else if(event.getAction() == MotionEvent.ACTION_UP) {
      // if user moves do not move the finger, update RatingBar value
      if(Math.abs(xDown - event.getX()) < 5) {
        return false;
      }
    }
    return true;
  }
}

Now simply add this listener to all your ratingbars in Adapters getView method:

ratingBar.setOnTouchListener(listenerRatingBar);

Voila, your ListView with RatingBars now works on Android 2.X.

10 Entrepreneurship Lessons Worth Thousands of Dollars

Instead of great success we have experienced great entrepreneurship lessons (for now). It also transformed me, a person who has …

Read article

Unique Czech words reflecting coronavirus now also with English explanations as Flashcard quiz in Vocabulary Miner

  Project Čestina 2.0 covering a variety of the modern Czech language with its slangs and new words has joined …

Read article

Performance of built-in higher-order functions Map, Filter, Reduce, and flatMap vs. for-in loop in Swift

  The most popular higher-order functions are map, filter, and reduce. We all use them since we think that syntax …

Read article

Contact