How to load Style Runtime for Ratingbar Android

779 views Asked by At

I have a fragment Feedback.java. there is a button which raises a popupwindow. I am creating a Rating bar in popup Window. My Custom Style for Rating bar is defined in XML files. i defined Style in values/styles.xml

<style name="RatingBar1" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/rating_bar_full</item>
        <item name="android:minHeight">48dip</item>
        <item name="android:maxHeight">48dip</item>

    </style>

I have created RatingBar runtime with above style using following code.

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            param.setMarginStart(15);
    float stepSize = (float) 0.5;
    rating = new RatingBar(getActivity(), null,R.style.RatingBar1);
    rating.setNumStars(5);          
    rating.setStepSize(stepSize);

but Rating bar is not showing. I know this is a problem that Styles are loaded onCreate() and we craete popup on ButtonClick. that is why Style is not applying. So how can i load Style at runtime for my RatingBar. Any Suggestion. Thanx Already!

2

There are 2 answers

0
SuperDude On BEST ANSWER

this worked for me. you can set any color to stars on runtime for RatingBr. Try this.

rbCustom = new RatingBar(getActivity());
            rbCustom.setNumStars(5);

            rbCustom.setStepSize(stepSize);
            LayerDrawable stars = (LayerDrawable) rbCustom.getProgressDrawable();
            stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.oceanBlue),Mode.SRC_ATOP);
0
Akash Moradiya On

Dynamically changing the style of views at runtime is not yet supported in Android, except for TextViews via setTextAppearance.

You have to set the style before the view gets drawn, either via XML or via Java in the constructor.

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