Question

I'm coding a chess board. For the layout, I include several "merged" layouts. I simply would like to access to the "merged" components from the Activity code, in order to change some of their properties (imageView background_color, imageView image path ...).

Edit : for example, I want to pass a row id (R.id.row_1), and initialize all components from this row id, with a for loop : the 8 cells are given each a different background color.

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeComponents(R.id.row_2);
    }

 public initializeComponents(int id){
       /* inflates the given layout child components
        * and set their properties
        */
 }

Here is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >    
<include layout="@layout/board"/>
</LinearLayout>

Here is my board.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
<include android:id="@+id/row_8" layout="@layout/board_line"/>
<include android:id="@+id/row_7" layout="@layout/board_line"/>
<include android:id="@+id/row_6" layout="@layout/board_line"/>
<include android:id="@+id/row_5" layout="@layout/board_line"/>
<include android:id="@+id/row_4" layout="@layout/board_line"/>
<include android:id="@+id/row_3" layout="@layout/board_line"/>
<include android:id="@+id/row_2" layout="@layout/board_line"/>
<include android:id="@+id/row_1" layout="@layout/board_line"/>

</TableLayout>

Here my board_line.xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<TableRow 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
>
<ImageView
        android:id="@+id/a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/cell_dimension"
        android:minHeight="@dimen/cell_dimension"
        android:maxWidth="@dimen/cell_dimension"
        android:maxHeight="@dimen/cell_dimension"
    />
    <ImageView 
        android:id="@+id/b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/cell_dimension"
        android:minHeight="@dimen/cell_dimension"
        android:maxWidth="@dimen/cell_dimension"
        android:maxHeight="@dimen/cell_dimension"
    />
    <ImageView 
        android:id="@+id/c"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/cell_dimension"
        android:minHeight="@dimen/cell_dimension"
        android:maxWidth="@dimen/cell_dimension"
        android:maxHeight="@dimen/cell_dimension"
    />
    <ImageView 
        android:id="@+id/d"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/cell_dimension"
        android:minHeight="@dimen/cell_dimension"
        android:maxWidth="@dimen/cell_dimension"
        android:maxHeight="@dimen/cell_dimension"
    />
    <ImageView 
        android:id="@+id/e"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/cell_dimension"
        android:minHeight="@dimen/cell_dimension"
        android:maxWidth="@dimen/cell_dimension"
        android:maxHeight="@dimen/cell_dimension"
    />
    <ImageView 
        android:id="@+id/f"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/cell_dimension"
        android:minHeight="@dimen/cell_dimension"
        android:maxWidth="@dimen/cell_dimension"
        android:maxHeight="@dimen/cell_dimension"
    />
    <ImageView 
        android:id="@+id/g"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/cell_dimension"
        android:minHeight="@dimen/cell_dimension"
        android:maxWidth="@dimen/cell_dimension"
        android:maxHeight="@dimen/cell_dimension"
    />
    <ImageView 
        android:id="@+id/h"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/cell_dimension"
        android:minHeight="@dimen/cell_dimension"
        android:maxWidth="@dimen/cell_dimension"
        android:maxHeight="@dimen/cell_dimension"
    />
</TableRow>

Thanks in advance for helps.

Was it helpful?

Solution

You can use LayoutInflater (or create Views completely programmatically) and combine all those to your needs. The following example should create some random colored 5x5 board. Since setting LayoutParams in code is ugly I would set them in xml and Inflate the layout.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main, null);
    TableLayout tl = (TableLayout) ll.findViewById(R.id.tableLayout);
    for (int j = 0; j < 5; j++) {
        TableRow tr = (TableRow) inflater.inflate(R.layout.table_row, null);
        for (int i = 0; i < 5; i++) {
            ImageView iv = (ImageView) inflater.inflate(R.layout.image_view, null);
            iv.setBackgroundColor(new Random().nextInt());
            tr.addView(iv);
        }
        tl.addView(tr);
    }
    setContentView(ll);
}

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </TableLayout>
</LinearLayout>

table_row.xml

<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TableRow>

image_view.xml

just one of those you defined in board_line.xml:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top