1

I need to load several ImageViews into a layout. The ImageViews are used to show a rating using 'star' icons. The number of stars (i.e. the rating is determined during loading by reading a configuration file.

The ImageViews are sitting inside a RelativeLayout layout.

Currently I am using something like this:

RelativeLayout l = ((RelativeLayout)(findViewById(R.id.RelativeLayout01)));
for (int nStarCount = 0; nStarCount < config.getAsInt("stars", 1); nStarCount ++) {
    ImageView image = new ImageView(this);
    image .setImageResource(R.drawable.star);
    image .setAdjustViewBounds(true); 
    l.addView(i); 
}

My quesions are:

1) Is doing the loading dynamically (not using an xml) is the 'right' way of doing this ?

2) If it is, how do I make the stars display in a line, currently they get placed one on top of the other.

Any help would be appreciated.

RM

2
  • have u got what u need in this post,? Commented Aug 17, 2011 at 7:47
  • @Roman M did you solve this issue? I have the same problem Commented Dec 6, 2018 at 13:03

3 Answers 3

3

If you know the maximum number of stars you would allocate, then you could use the built-in RatingBar view. Your XML just becomes something like:

<RatingBar android:id="@+id/ratingbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"/>

In your code, call ratingBar.setRating() to set the rating. You could set the max rating and actual rating to the same number if you don't want to see 'empty' stars. This way, you can save yourself the trouble of re-implementing effectively the same widget.

See: http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RatingBar

Sign up to request clarification or add additional context in comments.

Comments

2

For the dynamic view that you are creating, I don't think there is anything wrong with doing it via code.

However, if you prefer to layout via xml, you can do so and then adjust the visibility from code. I.e:

<RelativeLayout ...>
   ... stuff goes here
  <LinearLayout ..(setup where you want this
    orienation="horizontal">
     <ImageView id="@+id/star1"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star2"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star3"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star4"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
     <ImageView id="@+id/star5"
       android:src="@drawable/star"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>
  </LinearLayout>
</RelativeLayout>

The code actually looks a little more awkward, since you need to address each star imageView by id.

If you do a lot of these, you could make the code a bit less ugly by saving the id's into a list or something:

List<Integer> starIds = new List<Integer>();
starIds.add(R.id.star1);
starIds.add(R.id.star2);
...

for (int x=starCount; x<5; x++) {
  ImageView view = (ImageView)findById(starIds[x]);
  view.setVisiblity(GONE);
}

Comments

0

1) It's fine either way. Most people prefer XML though.

2) Put them in a LinearLayout with Orientation set to HORIZONTAL

4 Comments

But how can I do it with XML if I dont know how many stars are going to be there ? Isnt moving them to a LinearLayout ruin the orientation relative to the other layouts ? (that is why i was using a relativelayout)
Answer: you don't :). LinearLayout should be fine. The type of a layout only affects its contents, and Linear is fine here.
You said that its "fine either way", can you provide an example how to do it with an xml ?
No, because I don't use XML (or haven't to date).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.