Search code examples
androidratingbar

ratingbar can not show current numStars


I have a RatingBar and use the method setNumStars(final int numStars). in XML the code is like this :

 <RatingBar
                android:id="@+id/rating_bar"
                style="@style/CMYMyRatingBars"
                android:isIndicator="true"
                android:numStars="5"
                android:rating="5"
                />

then there is a problem . When the numStars is 0. My RatingBar will show 5 stars.


Solution

  • private RatingBar ratingBar;
    private float ratingValue;
    
    ratingBar = (RatingBar) findViewById(R.id.rating_bar);
    
    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
    
                @Override
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 
    
                    ratingValue = rating;
    
                }
            });
    
    ratingBar.getRating();
    
    
    
    <RatingBar
        android:id="@+id/rating_bar"
        style="@style/CMYMyRatingBars"
        android:isIndicator="true"
        android:numStars="0"
        android:rating="4"
         />
    

    Because you have given the rating 5 that is why it is showing 5 rating when you are giving 0 numStars. I have executed this code and code is working fine.You have to see zero rating then implement this code.

    <RatingBar
        android:id="@+id/rating_bar"
        style="@style/CMYMyRatingBars"
        android:isIndicator="true"
        android:numStars="0"
        android:rating="0"
         />
    

    styles

    <style name="CMYMyRatingBars" parent="@android:style/Widget.RatingBar">
            <item name="android:progressDrawable">@drawable/ratingbar</item>
            <item name="android:layout_width">wrap_content</item>
            <item name="android:layout_height">35dp</item>
            <item name="android:gravity">center</item>
        </style>
    

    ratingbar.xml

    <item
        android:id="@+android:id/background"
        android:drawable="@drawable/ratingbar_empty"/>
    <item
        android:id="@+android:id/secondaryProgress"
        android:drawable="@drawable/ratingbar_empty"/>
    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/ratingbar_filled"/>
    

    ratingbar_filled.xml

    <item android:drawable="@drawable/ratingbar_staron" android:state_pressed="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/ratingbar_staron" android:state_focused="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/ratingbar_staron" android:state_selected="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/ratingbar_staron"/>
    

    ratingbar_empty.xml

    <item android:drawable="@drawable/ratingbar_staroff" android:state_pressed="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/ratingbar_staroff" android:state_focused="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/ratingbar_staroff" android:state_selected="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/ratingbar_staroff"/>