2

What I am trying to do is create N (in this case 9) copies of the ImageView object R.id.tile, place each of them at different coordinates on the layout, and give each its own unique identifier.

board_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/backgroundcolor"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/topbar"
        ... >

        <ImageButton
            android:id="@+id/imagebutton"
            ... />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/board"
        android:layout_width="match_parent"
        android:layout_height="345dp"
        android:layout_centerVertical="true"
        android:background="@drawable/wwfboard" >

        <view class="languid.legend.xsolver.DrawView"
            android:id="@+id/myView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

        <ImageView
            android:id="@+id/tile"
            android:layout_width="21dp"
            android:layout_height="21dp"
            android:src="@drawable/tile" />
    </RelativeLayout>

</RelativeLayout>


BoardLayout.class:

@Override
protected void onCreate(Bundle savedInstance)
{   super.onCreate(savedInstance);
    setContentView(R.layout.board_layout);
    LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View[] tiles = new ImageView[9];
    entire_layout = (RelativeLayout)findViewById(R.layout.board_layout);


    for(int i = 0; i<tiles.length; i++){
        tiles[i] = (ImageView) inflater.inflate(R.layout.board_layout, null);
        tiles[i].setId(1000+i);
        params = new RelativeLayout.LayoutParams(-1, 345);
        params.leftMargin = 32*2*i;
        params.topMargin = 34*2*i;
        entire_layout.addView(tiles[i]);
    }
    ...

However, I keep getting "Source not found" errors on the last line: layout.addView(tiles[i]);..

Any ideas why?

(Side question: is RelativeLayout better than AbsoluteLayout if you're working with coordinates?)

LanguidLegend
  • 53
  • 2
  • 2
  • 6

1 Answers1

3

Edit:

First create a layout with one ImageView with the properties you like

Example:

create file in res/layout/singleimage.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/image2"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:src="@drawable/ic_launcher"
           android:contentDescription="Sample"/>

And then inflate the ImageView to get the copies of it like this

View[] tiles = new ImageView[9];
// get reference to LayoutInflater
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(int i = 0; i<tiles.length; i++) {
    //Creating copy of imageview by inflating it
    ImageView image = (ImageView) inflater.inflate(R.layout.singleimage, null);
    tiles[i] = image;
    tiles[i].setId(i);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-1, 345);
    params.leftMargin = 32*2*3;
    params.topMargin = 34*2*3;
    layout.addView(tiles[i]);
}

You have set id to i value here

tiles[i].setId(i);  

But you are (incorrectly) trying to get it from your other resource (the one you wanted to 'clone'):

layout.addView(tiles[i] = (ImageView) findViewById(R.id.tile)); // INCORRECT

Instead, set the id manually as above, and then:

layout.addView(tiles[i]);

And there is no need to call findViewById()

AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • Same result... stops on the `layout.addView(tiles[i]);` line and says **Source not found.** – LanguidLegend Apr 02 '13 at 13:55
  • @LanguidLegend I know that...you'll get exception that specified child has already parent. So have given a procedure how to do that.. do exactly like that..It will work for sure..Check my edit – Pragnani Apr 02 '13 at 17:35
  • so would R.layout.singleimage be the name of the XML file containing the ImageView, or just the innermost parent layer within the XML layout file? If there are multiple parent layers, does that make a difference in the java code? BTW Still gives me Source error.. I've updated my code to current. – LanguidLegend Apr 03 '13 at 15:45
  • R.layout.singleimage is the layout with single imageview in it...That should work if you take new layout with single imageview as I have shown in the answer.. – Pragnani Apr 04 '13 at 00:52
  • so in the line `tiles[i] = (ImageView) inflater.inflate(R.layout.board_layout, null);` why is the root set to `null`? Could it be anything else? – LanguidLegend Apr 14 '13 at 06:31
  • @LanguidLegend Because it is not group'ed under any layout – Pragnani Apr 14 '13 at 12:27