Search code examples
androidratingbar

Rating bar not showing all stars


I have the following code for RatingBar.

<RatingBar
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:rating="4"
    android:id="@+id/product_rating"
    android:isIndicator="false"
    android:numStars="5"
    android:stepSize="0.0"
    android:max="5"
    app:layout_constraintRight_toLeftOf="@+id/guideline4"
    tools:layout_constraintTop_creator="1"
    tools:layout_constraintRight_creator="1"
    android:layout_marginTop="8dp"
    tools:layout_constraintLeft_creator="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"/>

But even though I have used the following attributes to set the Max Stars and step size and max values, I should have got five visible stars. But.... I get the result like this:

Enter image description here

If I take a look at nexus 10 landscape I get this : enter image description here

What am I doing wrong?

Addition : I am using this layout again the same problem

<LinearLayout
    android:layout_width="0dp"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_weight="30">
    <RatingBar
        android:id="@+id/rating_bar_product_stats"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:max="5"
        android:numStars="5"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/rating_string_product_stats"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

Solution

  • Set android:layout_width="wrap_content" and not android:layout_width="0dp" Because if use android:layout_width="0dp" and set android:layout_weight="1" it may show more than 5 stars based on the device width.

    Update 1 As per your code above if I use this I get all 5 stars only

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
    android:layout_width="0dp"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_weight="30">
    <RatingBar
      android:id="@+id/rating_bar_product_stats"
      android:layout_width="wrap_content"
      android:layout_gravity="center"
      android:max="5"
      android:numStars="5"
      android:layout_height="wrap_content"/>
    <TextView
      android:text="4.4"
      android:id="@+id/rating_string_product_stats"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      />
    </LinearLayout>
    </LinearLayout>
    

    See the output below

    enter image description here