2

I'm able to create customized icon for rating bar, but I would like to add different rating bar in different situation programmatically.

Is there any way to achieve this rather than initialize in xml file?

nb369
  • 87
  • 7
  • You can use the third constructor for `RatingBar`, `RatingBar(Context context, AttributeSet attrs, int defStyle)` where you define the style resource – ataulm Apr 20 '14 at 15:26
  • Is it possible if I create the ratingbar in mylayout.xml, but want to apply style in code? – nb369 Apr 20 '14 at 15:30
  • Not as far as I know. You could explore `setProgressDrawable(int)` with the following answer to help with the creation of the drawables: http://stackoverflow.com/questions/15027475/constructing-a-ratingbar-using-images-loaded-from-the-web – ataulm Apr 21 '14 at 10:46

1 Answers1

0

I have a solution

ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(context, R.style.ProfileRatingBar);
RatingBar rate = new RatingBar(contextThemeWrapper, null, 0);

R.style.ProfileRatingBar is my custom ratingbar. Code below

<style name="ProfileRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/profile_ratingbar</item>
    <item name="android:minHeight">24dp</item>
    <item name="android:maxHeight">24dp</item>
</style>

Hope this helps you.

Pang
  • 9,564
  • 146
  • 81
  • 122
zozoname
  • 1
  • 1
  • 1