How to make RatingBar to show five stars

AndroidXmlLayout

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

This worked for me: RatingBar should be inside LinearLayout other than having layout width set to wrap content for RatingBar.

Solution 4 - Android

Additionally, if you set a layout_weight, this supersedes the numStars attribute.

Solution 5 - Android

If you are using

android:layout_width="match_parent"

Use wrap_content and it will only show the set numStars :)

android:layout_width="wrap_content"

Solution 6 - Android

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.

Solution 7 - Android

<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

Solution 8 - Android

I'm also facing the problem that exceeding the stars than specified no of stars. For this, we don't want worry about whatever the layout type either relative or linear layout. Simply use the width as follows:

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


Please avoid using match_parent and fill_parent for ratingbar.
Hope the things would be helpful.

Solution 9 - Android

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 !

Solution 10 - Android

For me, I had an issue until I removed the padding. It looks like there is a bug on certain devices that doesn't alter the size of the view to accommodate the padding, and instead compresses the contents.

Remove padding, use layout_margin instead.

Solution 11 - Android

If you are wrapping the RatingBar inside a ConstraintLayout with match_constraint for its width, the editor preview is going to show a number of stars proportional to its actual width, no matter what if you set android:numStars property. Use wrap_content to get the correct preview:

<RatingBar android:id="@+id/myRatingBar"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginStart="48dp"
           android:layout_marginEnd="48dp"
           android:numStars="5"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintTop_toBottomOf="parent" />

Solution 12 - Android

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

Solution 13 - Android

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

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" />

Solution 14 - Android

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

Solution 15 - Android

<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>

Solution 16 - Android

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

Solution 17 - Android

Another possibility is you are using the Rating in Adapter of list view

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

Below params are necessary for all the attributes to work properly:

  1. Set the root to parent
  2. boolean attachToRoot as false

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

Solution 18 - Android

The actual thing over here is to use wrap_content instead of match_parent. If you will use match_parent the layout will be filled with the stars even if it is more than the numStars variable.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRolandView Question on Stackoverflow
Solution 1 - AndroidAlex VolovoyView Answer on Stackoverflow
Solution 2 - AndroidLisandroView Answer on Stackoverflow
Solution 3 - AndroidSupunView Answer on Stackoverflow
Solution 4 - AndroidjsimonView Answer on Stackoverflow
Solution 5 - AndroidMQoderView Answer on Stackoverflow
Solution 6 - AndroidAdrián AlvarezView Answer on Stackoverflow
Solution 7 - AndroidAjith MemanaView Answer on Stackoverflow
Solution 8 - AndroidDevaView Answer on Stackoverflow
Solution 9 - AndroidReachmeDroidView Answer on Stackoverflow
Solution 10 - AndroidJohn LeeheyView Answer on Stackoverflow
Solution 11 - AndroidNicola GallazziView Answer on Stackoverflow
Solution 12 - AndroidNandan Kumar SinghView Answer on Stackoverflow
Solution 13 - AndroidGaurav ShettyView Answer on Stackoverflow
Solution 14 - AndroidKevin GaudinView Answer on Stackoverflow
Solution 15 - AndroidMuzikantView Answer on Stackoverflow
Solution 16 - AndroidJonView Answer on Stackoverflow
Solution 17 - AndroidDevrathView Answer on Stackoverflow
Solution 18 - AndroidNauman KhaliqView Answer on Stackoverflow