[android] 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

This question is related to android xml layout

The answer is


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.


<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


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.


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


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


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.


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"


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

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

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 !


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.


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.


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


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


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


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

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


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to layout

This view is not constrained What's the difference between align-content and align-items? CSS Flex Box Layout: full-width row and columns Fill remaining vertical space with CSS using display:flex What is setContentView(R.layout.main)? How to change line color in EditText Scrolling a flexbox with overflowing content UICollectionView - Horizontal scroll, horizontal layout? How to style a div to be a responsive square? 100% width Twitter Bootstrap 3 template