Thomas
Thomas

Reputation: 31

Add same ImageView several times to layout

I would like to add the same ImageView several times to my Layout during runtime. I tried to define the ImageView(size, position, ...) and add it with LAYOUT.addView(IMAGEVIEW). However, if I try to add it a second time (different position in the same layout), it does not work. It seems like the same reference-id of a bitmap can not be added twice to a layout.

I found the problem. My bitmaps were too big. If I reduce the sizes of the bitmaps, it works. Anyway, thanks for your help.

Upvotes: 3

Views: 2497

Answers (2)

Omar
Omar

Reputation: 463

This class should help:

public class MyImageView implements Cloneable {

public MyImageView(Context ctx){
    super(ctx);
}
public Object clone(){
    try{
        MyImageView obj = new MyImageView(this.getContext());
        obj.setImageDrawable(this.getDrawable());
        obj.setScaleType(this.getScaleType());
        try{
            obj.setLayoutParams(this.getLayoutParams());    
        }catch(Exception e){

        }

        obj.setId((int)(Math.random() * 100));

    } catch (CloneNotSupportedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return obj;
}

}

Upvotes: 2

Dave
Dave

Reputation: 6104

You cannot add the same instance of a View multiple times. You'll need to create a second ImageView using the same parameters as the first.

Upvotes: 1

Related Questions