Search code examples
androidlistviewratingbar

How to style RatingBar inside ListView? (Problems in API 21 and low)


I have a RatingBar inside a Relative Layout:

All Ok, grey and golden stars. https://www.dropbox.com/s/22y8117fwyv969x/Stars23.JPG?dl=0

But if I have the same RatingBar inside a ListView the result is:

What is the problem? How can I solve it? Any ideas?

Code:

<RatingBar
    android:id="@+id/ratings_list_item_ratingBar"
    style="@style/Widget.AppCompat.RatingBar.Small"
    android:theme="@style/RatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:isIndicator="true"
    android:numStars="5"
    android:stepSize="1"/>

Style in styles.xml:

<style name="RatingBar" parent="Theme.AppCompat">
    <item name="colorControlNormal">@android:color/darker_gray</item>
    <item name="colorControlActivated">@color/Yelow200</item>
</style>

Adapter:

public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater
                            .inflate(R.layout.ratings_list_item, parent, false);
            RatingBar ratingBar = (RatingBar) rowView.findViewById(R.id.ratings_list_item_ratingBar);
            float ratingNumber = (float)values.get(position).getRating();
            ratingBar.setRating(ratingNumber);
            return rowView;
    }

Solution

  • The LayoutInflater you are using may not be taking notice of your application's style.xml.

    Try (sorry, no way of testing this before, but it helped in a similar case) using the following LayoutInflater:

    LayoutInflater inflater = LayoutInflater.from(parent.getContext());