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
I found that the RatingBar
stretched to a maximum number of stars because it was enclosed in a Table with the attribute android:stretchColumns = "*"
.
Once I took stretchCoulumns
off all columns, the RatingBar
displayed according to the android:numStars
value
You should just use
numStars="5"
in your XML, and set
android:layout_width="wrap_content"
.
Then, you can play around with styles and other stuff, but the "wrap_content" in layout_width is what does the trick.
Even i was facing the issue @Roland , I had included one more attribute called
android:layout_alignParentRight="true"
in my RatingBar
declaration in XML. This attribute prevented from setting of the stars required and setting up the NumStars
Keep posted on the issues you come across !
To show a simple star rating in round figure just use this code
public static String getIntToStar(int starCount) {
String fillStar = "\u2605";
String blankStar = "\u2606";
String star = "";
for (int i = 0; i < starCount; i++) {
star = star.concat(" " + fillStar);
}
for (int j = (5 - starCount); j > 0; j--) {
star = star.concat(" " + blankStar);
}
return star;
}
And use it like this
button.setText(getIntToStar(4));
<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)
<RatingBar
android:id="@+id/ratingBar"
style="@style/custom_rating_bar"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:clickable="true"
android:numStars="5"
/>
Configure the number of Stars in the XML file... No need to provide it in Styles or Activity/Fragment .... IMPORTANT: Make sure you Put the WIDTH as Wrap content and weights are not enabled