tags:

views:

41

answers:

2

Hi all,

I am stuck in an peculiar scenario, while developin an app in android.

My app, downloads image from internet and populates the same in a thumbnail format in the application.

As i am not aware of the number of picture's/thumbnails at design time, i am creating image view controls dynamically by using the below mentioned code.

ArrayList<String> parsedPhoto=oPhotoList.getPhoto();                                  
RelativeLayout media = (RelativeLayout)findViewById(R.id.media);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                          RelativeLayout.LayoutParams.MATCH_PARENT,       
                         RelativeLayout.LayoutParams.WRAP_CONTENT);

       for(int iCounter=0;iCounter<parsedPhoto.size();iCounter++)
       {
         bitmap=oParser.DownloadImage(parsedPhoto.get(iCounter));
        ImageView img = new ImageView(this);
         img.setImageBitmap(bitmap); 
         media.addView(img,p);}

But in this scenario, all my images gets overlapped with each other. Wherein i want 3 images to be displayed on each row, which i am able to achieve through xml.

Can somebody help me out.

A: 

Instead of using RelativeLayout, use GridView and an adapter. Not only will it make it a lot more efficient, it will give you the ability to add many other features (don't download image if its already there, download meta data, etc)

Sameer Segal
A: 

The problem is that you are not locating the imageviews in the RelativeLayout. With this layout you have to specify where you want to locate each element. If you don't they will located on the same position.

There are views like GridView which lets you do this in a easier way.

However, you can use a RelativeLayout if you want to. You can use the addRule() method of RelativeLayout.LayoutParams class to specify the location of each image. For example:

Firstly you can store the last ids of the added images.

// The id of the first image added in the last row
int lastFirstId;
// The id of the last image added
int lastId;

Then, create and specify the position of the imageview (with a couple of exceptions for the first images):

RelativeLayout.LayoutParams params = new RelativeLayoutParams();
params.setRule (params.RIGHT_OF, lastId);
params.setRule (params.BELOW, lastFirstId);
img.setLayoutParams(params);

I hope it helps

Urizev