Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make RatingBar to show five stars

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

like image 761
Roland Avatar asked Oct 04 '10 19:10

Roland


People also ask

How to set 5 star in rating bar in Android?

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.

How to get rating from RatingBar in Android?

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();

How to use rating stars in Android Studio?

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.


2 Answers

<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) 
like image 198
Alex Volovoy Avatar answered Sep 30 '22 14:09

Alex Volovoy


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.

like image 43
Lisandro Avatar answered Sep 30 '22 12:09

Lisandro