Search code examples
androidratingbar

Android ratingBar ignoring numStars


I'm very new to Android development. I'm trying to add a ratingBar indicator to a layout screen that will be a ListView item, so the user sees a list of restaurant names with their star values to the right of each.

I want to limit the view to 5 stars - i.e. it should never display more than 5 stars, and 5 only if the user has rated the restaurant as 5 stars (in another activity.)

I've read this, this, and this, as well as the developer.android.com documentation for ratingBar, and I cannot for the life of me get it to display only 5 stars.

Here's my layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="64dp"
                android:paddingRight="32dp"
                android:paddingTop="4dp"
                android:paddingBottom="4dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/restaurantNameLabel"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:textColor="#000000"
        android:textSize="16sp"
        tools:text="Shamrock Club"
        />

    <RatingBar
        android:id="@+id/ratingBar"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:numStars="5"
        android:stepSize=".01"
        android:isIndicator="true"
        android:rating="3"
        android:layout_toEndOf="@id/restaurantNameLabel"/>
</RelativeLayout>

And this is what it looks like in the Android Studio Design view; note there are 6+ stars displaying. What am I missing here?

enter image description here


Solution

  • If you can change from a relative layout to a linear layout this should work just fine

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="64dp"
                    android:paddingRight="32dp"
                    android:paddingTop="4dp"
                    android:paddingBottom="4dp">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/restaurantNameLabel"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:textColor="#000000"
            android:textSize="16sp"
            tools:text="Shamrock Club"
            />
    
        <RatingBar
            android:id="@+id/ratingBar"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:numStars="5"
            android:stepSize=".01"
            android:isIndicator="true"
            android:rating="3"
            android:layout_toEndOf="@id/restaurantNameLabel"/>
    
    </LinearLayout>
    

    The way it is now it will actually only show 3 stars but you can change android:rating to reflect what you would like.