How to make RatingBar to show five stars

I follow the standard examples of adding RatingBar . To control the number of stars, I tried using android:numStars="5" . The problem is that the number of stars does nothing at all. In the portrait manner, 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) ), which loads the xml, but also was not successful with this option.

So my question is: how to define your layout so that it displays only five stars? Install numStars doesn't seem to work.

Thanks in advance, Roland

+84
android xml layout
04 Oct '10 at 19:50
source share
16 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) 
+147
Oct 04 2018-10-21
source share

The RatingBar.setNumStar documentation says:

Sets the number of stars to show. In order for them to display correctly, it is recommended that the width of the layout of this widget be portable.

Therefore, setting the layout width to wrap_content should solve this problem.

+53
Sep 11 '11 at 18:30
source share

This worked for me: the RatingBar should be inside the LinearLayout , except that the layout width is set to wrap the content for the RatingBar .

+25
Jan 24 '12 at 7:59
source share

In addition, if you set layout_weight , this will replace the numStars attribute.

+10
May 30 '12 at 15:45
source share
 <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" /> 

Set the number of stars in the XML file ... You do not need to specify it in the styles or action / fragment .... IMPORTANT: make sure you put WIDTH as the Wrap content, and the scales are not included.

+5
Nov 22 '12 at 13:57
source share

I also encounter a problem that is superior to stars than non-stars indicated. To do this, we need not worry about which type of layout is relative or linear. Just use the width as follows:

 ratingBar.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 


avoid using match_parent and fill_parent for ratingbar .
I hope that everything will be useful.

+5
Sep 16 '14 at 8:59
source share

If you use

android:layout_width="match_parent"

Use wrap_content and it will only show set numStars :)

android:layout_width="wrap_content"

+4
Dec 06 '16 at 11:28
source share

You should just use numStars="5" in your XML and set android:layout_width="wrap_content" .
Then you can play around with styles and stuff, but the "wrap_content" in layout_width is what the trick does.

+4
Jan 17 '18 at 18:48
source share

Even I ran into @Roland's problem, I included another attribute

 android:layout_alignParentRight="true" 

in the RatingBar in XML. This attribute prevented the setting of the required stars and the setting of NumStars

Keep a record of the problems you are facing!

+3
Apr 05 '11 at 6:11
source share

For me, I had a problem until I removed the gasket. It seems that on some devices there is an error that does not resize the view to accommodate the add-on and compresses the content instead.

Remove padding , use layout_margin .

+2
Oct 06 '15 at 18:11
source share

The default value is set with andoid: rating in the xml layout.

+1
04 Oct 2018-10-10T00:
source share

To show a simple star rating in a round shape, simply 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));

+1
May 12 '16 at 6:46 a.m.
source share

You can add a default rating of five stars to the xml layout side

 android:rating="5" 

Edit:

 <RatingBar android:id="@+id/rb_vvm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="@dimen/space2" android:layout_marginTop="@dimen/space1" style="@style/RatingBar" android:numStars="5" android:stepSize="1" android:rating="5" /> 
+1
Dec 14 '16 at 10:32
source share
 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <RatingBar android:id="@+id/ruleRatingBar" android:isIndicator="true" android:numStars="5" android:stepSize="0.5" style="?android:attr/ratingBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 
0
Jun 07 2018-12-12T00:
source share

I found that RatingBar stretched to the maximum number of stars, because it was enclosed in a table with the android:stretchColumns = "*" attribute android:stretchColumns = "*" .

As soon as I removed stretchCoulumns from all columns, RatingBar displayed according to android:numStars

0
Jul 17 '12 at 17:23
source share

Another possibility is that you use rating in the list view adapter

 View android.view.LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot) 

The following are the parameters necessary for the proper operation of all attributes:

  • Set root to parent
  • boolean attachToRoot as false

Example: convertView = inflater.inflate (R.layout.myId, parent, false);

0
Nov 18 '14 at 5:27
source share



All Articles