6

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

user4157124
  • 2,809
  • 13
  • 27
  • 42
d-man
  • 57,473
  • 85
  • 212
  • 296
  • 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
  • 30,199
  • 37
  • 136
  • 151
silver_mx
  • 828
  • 9
  • 16
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
  • 2,167
  • 1
  • 18
  • 25
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,371
  • 1
  • 11
  • 19