Need some help with implementing/understanding how the RatingBar works.
In my Activity I inflate my xml which has a RatingBar in it multiple times. Something like this. Stripped out version is
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(contentView);
// there are other views here sooo had to do it this way.
ViewGroup ratingsContainer = (ViewGroup) findViewById(R.id.x11);
for(int i = 0 ; i < 5; i++){
ratingsContainer.addView(createRatingBar(i));
}
}
private RatingBar createRatingBar(int index){
RatingBar ratingBar = (RatingBar)getLayoutInflater().inflate(R.layout.ratingbar, null);
ratingBar.setRating(index);
return ratingBar;
}
My ratingbar xml
<RatingBar xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:numStars="5"
android:stepSize="1"/>
my activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/x11"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
</LinearLayout>
But it does not seem to work properly 1. The number of stars it shows is more than the number of stars. More than 5 2. The data does not hold on rotation.
What do you think might be the problem?