Reputation: 6711
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
Upvotes: 1
Views: 8554
Reputation: 21883
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
Upvotes: 3
Reputation: 46844
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);
}
Upvotes: 2
Reputation: 33901
1) It's fine either way. Most people prefer XML though.
2) Put them in a LinearLayout with Orientation set to HORIZONTAL
Upvotes: 0