Skip to content Skip to sidebar Skip to footer

Set Lower Limit Of Number Of Stars Shown In Ratingbar Android

With the RatingBar widget in android you can set the number of stars to be show, but is there a way of setting a lower limit on the number of stars that are always active. As an ex

Solution 1:

If this is not possible in the framework strictyl speaking, you could put a default value of 1 with android:rating set to 1, and the add a listener on the RatingBar to go back to 1 when the user tries to go lower no?

Solution 2:

is there a way of setting a lower limit on the number of stars that are always active.

Yes you can set the Rating using setRating() method. For example:

ratingBar.setRating(rat);

As an example rating a film or something where you cannot submit a rating of 1?

For this, you have to implement a condition inside the onRatingChanged() method. for example:

ratingBar.setOnRatingBarChangeListener(newOnRatingBarChangeListener() {

    @OverridepublicvoidonRatingChanged(RatingBar ratingBar, float rating, 
      boolean fromUser) {
        // implement your condition here
    }
});

Solution 3:

   <RatingBar android:id="@+id/ratingbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"
    android:rating="1"/>

By setting rating =1 it is shows rating 1.

Solution 4:

As mentioned by @sephy supporting his answer. I am adding a sample code which I have used in my app.

mRatingBar.setOnRatingBarChangeListener(newRatingBar.OnRatingBarChangeListener() {
        @OverridepublicvoidonRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            if(selectedRating<=0) {
                mRatingBar.setRating(1);
            }
       }
    });

Post a Comment for "Set Lower Limit Of Number Of Stars Shown In Ratingbar Android"