Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setNumStars and setRating does not work in custom RatingBar

Tags:

I have a problem with seting a custom ratingBar in Java. The problem is that setNumStars and setRating are not working. I just get one star on the screen which is fully marked although I've set the rate to 0.0f.

Here's the code:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

chapter1 = new Chapter(this);
android.widget.RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(metrics.widthPixels , metrics.heightPixels);
myLayout.addView(chapter1, layoutParam);

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
        RatingBar ratingBar = new RatingBar(this);
        ratingBar.setProgressDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.custom_ratingbar_menu, null));
        ratingBar.setNumStars(3);
        ratingBar.setRating(0.0f);
        layoutParam = new RelativeLayout.LayoutParams(metrics.widthPixels / 6 , metrics.heightPixels / 32);
        layoutParam.setMargins(70 + chapter1.getLeft() + wSize * j, 50 + chapter1.getTop() + hSize * i + (metrics.heightPixels / 6), 0, 0);
        myLayout.addView(ratingBar, layoutParam);
    }
}

And we have used the accepted answer in How to create Custom Ratings bar in Android for custom_ratingarbar_menu

Here's custom_ratingbar_menu :

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+android:id/background"
        android:drawable="@drawable/customm_empty_menu" />
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/customm_empty_menu" />
    <item android:id="@android:id/progress"
        android:drawable="@drawable/custom_full_menu" />
</layer-list>

And it's customm_empty_menu :

<?xml version="1.0" encoding="utf-8"?>

<selector
xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@drawable/stars_off_menu" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@drawable/stars_off_menu" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@drawable/stars_off_menu" />

    <item android:drawable="@drawable/stars_off_menu" />

</selector>

And this is custom_full_menu :

<?xml version="1.0" encoding="utf-8"?>

<selector
xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@drawable/stars_on_menu" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@drawable/stars_on_menu" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@drawable/stars_on_menu" />

    <item android:drawable="@drawable/stars_on_menu" />

</selector>
like image 512
Arya Sadeghi Avatar asked Jun 30 '17 07:06

Arya Sadeghi


People also ask

How to use Rating bar in Android studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have declared Rating bar and set numStars as 5 means it allows maximum number of stars 5 and button.


1 Answers

Edit There is some problem with setting the progress drawable in code. Your drawables work when used in XML. The problem seems to revolve around setting the appropriate attributes. If I get time, I can look into this further or maybe someone else has an idea. (Check your XML, there is a typo: "customm" instead of "custom". I have corrected it below.)

(For API level 21 and above, you could do the following:

In MainActivity.java:

RatingBar ratingBar = new RatingBar(this, null, 0, R.style.myRatingBar);

In styles.xml:

<style name="myRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/custom_ratingbar_menu</item>
</style>

But this is not a general solution, so I prefer the following.)

In the meantime, you can accomplish what you want by inflating a RatingBar from XML and adding it to your layout instead of doing new RatingBar(this). In this way, you can make use of the attributes in the XML file. This fixes your issue.

Define an XML layout file that contains just a rating bar as follows:

rating_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RatingBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/modelRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:progressDrawable="@drawable/custom_ratingbar_menu" />

The drawable files are much as you defined them. I use a different icon since I did not have access to yours:

custom_ratingbar_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/custom_empty_menu" />
    <item
        android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/custom_empty_menu" />
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/custom_full_menu">
    </item>
</layer-list>

custom_empty_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@android:drawable/star_off" />
    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@android:drawable/star_off" />
    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@android:drawable/star_off" />
    <item android:drawable="@android:drawable/star_off" />

</selector>

custom_full_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@android:drawable/star_on" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@android:drawable/star_on" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@android:drawable/star_on" />

    <item android:drawable="@android:drawable/star_on" />
</selector>

Finally, the main activity's onCreate():

MainActivity#onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    RelativeLayout myLayout = new RelativeLayout(this);

    super.onCreate(savedInstanceState);
    setContentView(myLayout);
    // Your loop would go here...
    RatingBar ratingBar = (RatingBar) getLayoutInflater()
        .inflate(R.layout.rating_bar, myLayout, false);
    myLayout.addView(ratingBar);
    ratingBar.setNumStars(5);
    ratingBar.setStepSize(0.5f);
    ratingBar.setRating(2.0f);
}

Old answer, but still holds.

From the documentation for RatingBar:

The number of stars set (via setNumStars(int) or in an XML layout) will be shown when the layout width is set to wrap_content (if another layout width is set, the results may be unpredictable).

You are doing the following. (The width and height are not wrap_content.)

layoutParam = new RelativeLayout.LayoutParams(metrics.widthPixels / 6 , metrics.heightPixels / 32);
myLayout.addView(ratingBar, layoutParam);

Doing the following, instead, may get you what you want, or at least, get you the number of stars you want.

layoutParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
myLayout.addView(ratingBar, layoutParam);

I suggest that you forego the progress drawable for the time being and get the number of stars and rating right then introduce the drawable.

If this doesn't work, can you post custom_ratingbar_menu?

like image 103
Cheticamp Avatar answered Oct 04 '22 14:10

Cheticamp