3

I have an ImageView object, R.id.tile, defined in my XML layout, and what I'm trying to do is create clones of it and place each of them at different coordinates.

This is what I have so far:

    protected void onCreate(Bundle savedInstance)
    {   super.onCreate(savedInstance);
        setContentView(R.layout.board_layout);
        layout = (AbsoluteLayout)findViewById(R.id.board);
        img = (ImageView)findViewById(R.id.tile);
        View[] tiles = new ImageView[9];
        for (int i = 0; i<tiles.length; i++) {
            tiles[i] = (ImageView)findViewById(R.id.tile);
        }

        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                tiles[i+j].setX((float) 32*2*i);
                tiles[i+j].setY((float) 34.39*2*j);
            }
        }
     ...

But when I am debugging it keeps stopping on the line tiles[i] = (ImageView)findViewById(R.id.tile);
with the error "Source not found."

Any ideas?

LanguidLegend
  • 53
  • 2
  • 2
  • 6
  • Is there a specific reason why you are trying to use clones of the original ImageView rather than making new ones and setting their Image to be the same? I think the latter approach would be far easier. You can then call `layout.addView(tiles[i]);` to add the new ones to your layout. – FoamyGuy Mar 28 '13 at 20:24
  • 1
    @FoamyGuy May be he don't want to set all the properties or styles acquired by original imageview like height, widht,..etc for all of them programmatically. – Pragnani Mar 28 '13 at 20:32
  • 1
    @Pragnani possibly, and if that is the case then he can declare the ImageView in its on layout.xml file and then use LayoutInflater to create the new ones, that will make them all be pre-set to the desired configuration. – FoamyGuy Mar 28 '13 at 20:33
  • @FoamyGuy Yes...Agreed.. – Pragnani Mar 29 '13 at 05:11

2 Answers2

2

activity_main.xml

<LinearLayout
    android:id="@+id/linear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
</LinearLayout>

MainActivity.java

ImageView iv;
LinearLayout linear;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    linear = new LinearLayout(this);
    linear = (LinearLayout)findViewById(R.id.linear);

    for(int i=1;i<10;i++)
    {
        iv = new ImageView(this);
        iv.setImageResource(R.drawable.plus);
        iv.setPadding(0,0,0,20);
        linear.addView(iv);
    }
}

app view look like this app view

sohag
  • 21
  • 3
-1
ImageView imageview=new ImageView(context);

imageview=yourimageview // copy of your original

For your problem try this

View[] tiles = new ImageView[9];
ImageView testview= (ImageView)findViewById(R.id.testview);

for (int i = 0; i<tiles.length; i++) {
            tiles[i] = new Imageview(context);
        }
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • I used these changes, and no more error (yay!), but only the 9th instance (last element) of `tiles` shows up on the layout. I'm guessing this is because every element of `tiles` array points to the same object? – LanguidLegend Mar 29 '13 at 01:03
  • @AdamChoate Yeah...Take a separate layout with single imageview in it.. and then inflate the ImageView to get the copy of it.. and use that image..as FoamGuy Suggested – Pragnani Mar 29 '13 at 05:16
  • Do I have to create a new layout for every instance of the imageview? Could you show me how? – LanguidLegend Mar 29 '13 at 14:06
  • Thanks for your quick responses :) Anyway I've opened a new question regarding how to inflate multiple view objects: [How to inflate new copies of an ImageView object on a layout?](http://stackoverflow.com/questions/15719674/how-to-inflate-new-copies-of-an-imageview-object-on-a-layout) – LanguidLegend Mar 31 '13 at 05:27
  • 4
    The code doesn't make sense. U r assigning tiles[i] twice. The instruction "tiles[i] = new Imageview(context);" should have no effect – M. Usman Khan Feb 24 '15 at 12:28