0

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?

user3547028
  • 149
  • 2
  • 8

2 Answers2

1
  1. The number of stars it shows is more than the number of stars. More than 5

Quote from the documentation:

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

Source: http://developer.android.com/reference/android/widget/RatingBar.html

  1. The data does not hold on rotation.

When you rotate the screen, the onCreate method is called, recreating them. You can either use onSaveInstanceState and onRestoreInstance state to save/restore the ratings, or you can add them via XML in your activity_main.xml file and let Android do that for you.

EDIT: The problem with the width was related with using null when inflating the layout.
To fix it:

ViewGroup ratingsContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        // there are other views here sooo had to do it this way.
        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, ratingsContainer, false);
        ratingBar.setRating(index);
        return ratingBar;
    }

More info about why you shouldn't use null when inflating here.

For your second problem (rotation):
You can put the RatingBars in the activity_main file, eliminating the need for createRatingBar function.

<LinearLayout
        android:id="@+id/x11"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <RatingBar
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:numStars="5"
            android:stepSize="1" />

        <RatingBar
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:numStars="5"
            android:stepSize="1" />
    </LinearLayout>
LordRaydenMK
  • 13,074
  • 5
  • 50
  • 56
  • As you can see the layout_width is set to wrap_content. So I dont know why you would say that. Second, I also have a text view there which manages to hold on to its data. Evevery time onCreate is called I fill up the data again. I dont need to do anything else. – user3547028 Jun 10 '14 at 20:03
  • can you post your `activity_main.xml` layout? – LordRaydenMK Jun 10 '14 at 20:42
  • This did not resolve it. But that was nice to know. Your rotation solution I can't use RatingBar like that. The for loop has a 5 there. can sometimes have 2 can sometimes have 10 so I need to inflate that view the same way. – user3547028 Jun 10 '14 at 22:35
  • then you can override `onSaveInstanceState` and `onRestoreInstance` to save/restore the values. The system does it automatically for views inflated via XML. – LordRaydenMK Jun 11 '14 at 06:46
  • Or you can try adding an ID to the rating bar (int value grater than 0) by calling `setId` – LordRaydenMK Jun 11 '14 at 07:08
  • ALthough your solution did not help me it was nice to read the inflating part plus you tried to help me out a lot. Thanks :) It will definitely help me out in the future. I figured the problem out, basically the Rating bar saves the values, setting it to false solved my problem – user3547028 Jun 11 '14 at 16:44
0

The data does not hold on rotation.

This is because the Activity/the fragment is created new, when the orientation changes. You have to store the state - there are many good answers around the www. One is this: Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
D4ngle
  • 21
  • 7