MasterCrumble
MasterCrumble

Reputation: 111

Android RatingBar setRating doesn't work

I am trying to display an int value as a ratingBar. The max value is 5.

This is my XML file:

<RatingBar
            android:id="@+id/ratBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:isIndicator="false"
            android:numStars="5" />

But if I set the rating with setRating(), android draws a lot of stars. I can't figure out how many because my screen is too small.

Could anybody tell me what's wrong?

Upvotes: 3

Views: 8561

Answers (4)

Debanjan
Debanjan

Reputation: 2836

This happenswhen the drawable you set in your style for the rating bar has diffent size of png for background, secondary progress and progress.

Change it to something like

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
        android:drawable="@android:color/transparent" />
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/star_half" />
    <item android:id="@android:id/progress"
        android:drawable="@drawable/star_full" />

Upvotes: 0

Alpay
Alpay

Reputation: 1368

I couldn' t understand what exactly your problem is, but using RatingBar in Android applications is quite easy anyway. You need to define your RatingBar in a proper location;

<RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:isIndicator="true"
        android:max="5"
        android:numStars="3" />

get a reference to it, within your activity;

private RatingBar rb;
...
this.rb = (RatingBar) findViewById(R.id.ratingBar1);

and start to set values to fill the stars;

this.rb.setRating(2); , this.rb.setRating((float)2.5); etc.

If your screen is too small to display five stars, you can set the number of stars, just as I did above. And you have several options;

1) You can put two ratingbars one under the other one with 3 and the other with 2 stars. When your number exceeds 3, you can continue filling the 2 stars of the bottom ratingbar.

2) If possible, you can set float values instead of integers. So that you can set 1, 1.5, 2, 2.5, 3, 3.5, etc. and you can fill 3 stars for your interval 1-5 after a little algebra.

Upvotes: 3

Janmejoy
Janmejoy

Reputation: 2731

you can try this also

<RatingBar
    android:id="@+id/ratingBar"
    style="?android:attr/ratingBarStyleIndicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/artist"
    android:layout_below="@+id/artist"
    android:numStars="5"
    android:rating="2.0"
    android:stepSize="1.0" />

Upvotes: 2

GrIsHu
GrIsHu

Reputation: 23638

You need to define your own custom styles to achieve what you want for the RatingBar. Try out creating the styles and apply that style in your RatingBar view.

Like this:

<?xml version="1.0" encoding="utf-8"?>
  <resources>
    <style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/food_ratingbar_full</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
  </style> 
 </resources> 
 <RatingBar   android:id="@+id/my_rating_bar"
        ...
        style="@style/foodRatingBar" />

Check out here for more details.

Upvotes: 2

Related Questions