Search code examples
androidratingbar

How do I make "9/10" a "4.5/5" on an android RatingBar?


The Rating bar in Android has the attributes numStars to represent the total number of stars displayed, and the attribute stepSize to represent the granularity of scale.

However, I want to have a rating scale of 10 mapped to a RatingBar of 5 stars, so that a 9/10 is shown on the scale as 4.5/5 stars. Is this possible for the android Rating Bar?


Solution

  • Divide the number by two in order to end up with your value over 5:

    In your case it is 9, so it's 9/2 = 4.5.

    Then, pass that value to your ratingBar:

    // Retrieved Json Object. float might be redundant 
    //if getNumber is already of type float
    float ratingValue = (float) object.getNumber()/2;
    
    // This is used to set the rating value
    myRatingBar.setRating(ratingValue); 
    
    // This is used to show your stars
    myRatingBar.setStepSize(ratingValue);