If I define a layout using XML, but I want to populate it with some number n, of ImageViews, hwhat is the best way to go about doing this?
If I wanted to put in a single IMageView, I could easily put it in the XML of the layout, but if n (the number) is only known at runtime, I can’t really add this to the XML script (can I)?
The two ways I have so far (neither of which I like) are:
- Forget XML, just do the whole layout in Java. Laborious
-
Add the ImageViews in the Java code thus:
thelayout = (LinearLayout)findViewById(R.id.mylayout);
setContentView(thelayout);
for(int i = 0; i < n;i++){
thelayout.addView(new ImageView(this));
}
Define your layout in XML with a LinearLayout (or even better, a ListView, ScrollView or something like that) somewhere, then in code add your ImageViews to this layout. This way you can add the images in code and define the rest of the UI in XML.