I am following the standard example of how to add a RatingBar
. To control the number of stars I tried to use android:numStars="5"
. The problem is that the number of stars doesn't seem to do anything at all. In portrait-layout I get 6 stars and when I flip the phone I get about 10 stars. I tried to set the number of stars in my Activity (myBar.setNumStars(5)
) that loads the xml but there was no success with that option either.
So my question is how do I define my layout so that it will only show five stars? Set numStars
doesn't seem to work.
Thanks in advance, Roland
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have declared Rating bar and set numStars as 5 means it allows maximum number of stars 5 and button.
Get Android RatingBar Value In android, by using RatingBar methods (getNumStars(), getRating()) we can get the number of stars and the rating value which was selected. Following is the code snippet to get the rating details from RatingBar in android applications. int noofstars = rBar. getNumStars();
A user can simply touch, drag or click on the stars to set the rating value. The value of rating always returns a floating point number which may be 1.0, 2.5, 4.5 etc. In Android, RatingBar is an extension of ProgressBar and SeekBar which shows a rating in stars.
<RatingBar android:id="@+id/rating" android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/ratingBarStyleSmall" android:numStars="5" android:stepSize="0.1" android:isIndicator="true" />
in code
mRatingBar.setRating(int)
The RatingBar.setNumStar
documentation says:
Sets the number of stars to show. In order for these to be shown properly, it is recommended the layout width of this widget be wrap content.
So setting the layout width to wrap_content
should solve this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With