0

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.

loloof64
  • 5,252
  • 12
  • 41
  • 78
  • you could try it like this: `findViewById(R.id.row_3).findViewById(R.id.b)` – zapl Feb 16 '12 at 15:52
  • Thank you. But do you know how I can intialize all components of a line (R.id.row_2), without manually mentionning each child component, but with a for/while loop ? Thanks in advance. – loloof64 Feb 16 '12 at 15:56

1 Answers1

0

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:
zapl
  • 63,179
  • 10
  • 123
  • 154
  • Thnak you very much :) I agree it is an ugly way to do by code : but it is because i want to make a ChessBoard, and i am still searching for the best graphical solution : full code by overriding View or code and layout. (I also want to do some "drag and drop" : unfortunately not available as is on api 3) – loloof64 Feb 16 '12 at 20:31