SetNumStars and setRating do not work in custom RatingBar

I am having a problem setting custom ratingBar in Java. The problem is that setNumStars and setRating do not work. I just get one star on the screen that is fully marked, although I set the speed to 0.0f.

Here is 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 used the accepted answer in How to create a custom ratings panel 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 this is 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> 
+5
source share
1 answer

Edit There is some problem with setting the progress available in the code. Your drawings work when used in XML. The problem seems to be related to setting the appropriate attributes. If I get the time, I can study this further, or maybe someone has an idea. (Check your XML, there is a typo: “customm” instead of “custom.” I fixed it below.)

(For API level 21 and above, you can 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 the RatingBar from XML and adding it to your layout instead of doing a new RatingBar(this) . This way you can use attributes in the XML file. This fixes the problem.

Define an XML layout file that contains only the evaluation panel 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 files you select are the same as you defined them. I am using 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 onCreate() action:

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 preserved.

From the documentation for RatingBar :

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

You do the following. (Width and height are not wrap_content .)

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

By doing the following, instead you can get what you want, or at least get the number of stars you want.

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

I suggest you abandon the progress that is currently available, and get the number of stars and rating on the right, and then submit available.

If this does not work, can you send custom_ratingbar_menu ?

+3
source

Source: https://habr.com/ru/post/1269359/


All Articles