Home » Android » android – How to make RatingBar to show five stars

android – How to make RatingBar to show five stars

Posted by: admin March 11, 2020 Leave a comment

Questions:

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

How to&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)

Answer:

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.

Answer:

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

Answer:

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

Answer:

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.

Answer:

<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

Answer:

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.

Answer:

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"

Answer:

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 !

Answer:

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.

Answer:

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

Answer:

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

Answer:

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

Answer:

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

Answer:

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

Answer:

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

Answer:

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

Answer:

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.