6

I don't see any method like setStyle for class RatingBar. How can I set the rating style from code?

user4157124
  • 2,533
  • 12
  • 24
  • 39
d-man
  • 55,965
  • 82
  • 204
  • 290
  • 1
    Duplicate: http://stackoverflow.com/questions/2016249/how-to-programmatically-setting-style-attribute-in-a-view – Casebash Jun 01 '10 at 06:06
  • 5
    NOT a duplicate. A ratingbar has different properties and functionality. Things like setting the star images, star height, etc WILL NOT be answered by that question. – StackOverflowed May 23 '12 at 20:30

3 Answers3

13

This works for me (source):

RatingBar rating =
  new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);
Pops
  • 29,209
  • 36
  • 130
  • 150
silver_mx
  • 743
  • 8
  • 15
2

You need to set it in xml file! If you like to change it on the run, you can have two different View-s (rb) and switch them in the program (add/remove from layout) or use rb1.setVisibility(View.VISIBLE) and rb2.setVisibility(View.GONE);

<RatingBar 
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:layout_alignParentRight="true" android:id="@+id/rb1"
    style="?android:attr/ratingBarStyleSmall"
    android:clickable="false" android:numStars="5">
</RatingBar>
MatejC
  • 1,957
  • 1
  • 16
  • 21
1

If you are programmatically creating the RatingBar as follows the

RatingBar rating = new RatingBar(context, null, android.R.attr.ratingBarStyleSmall);

then don't for get to add

rating.setIsIndicator(false);

else the touch will not work on RatingBar.

The android.R.attr.ratingBarStyleSmall gives you very small stars if you want to have a slightly bigger stars use android.R.attr.ratingBarStyleIndicator

akhil nair
  • 1,127
  • 1
  • 9
  • 19