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>