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);
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 ?