0

I built a layout like this in XML say "block.xml" block.xml

Now, i want to create an XML like this enter image description here

so that i can access each block as an array. Can somebody tell me how can i use block.xml as a template to generate my new xml file to be put as a UI part in android. Thank You

Just to be sure , i want to use table layout i dont know how to procees.

Prakhar
  • 530
  • 10
  • 24

2 Answers2

1

In your layout xml you may use the include tag and re-use your block.xml
as:

<include
        android:id="@+id/block1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        layout="@layout/block" />   

And in your java code you can access views from block.xml as

View firstBlock = findViewById( R.id.block1 );
View blockButton = firstBlock.findViewById( R.id.someButtonId );
Zeba
  • 3,041
  • 1
  • 28
  • 39
  • Hey, thanks for the answer .. . i need to know suppose if in the layout which i am using inside include tag, i want to access some of the items defined that xml. .. can i do it? It would be very helpful – Prakhar Jan 20 '15 at 07:32
  • @Prakhar yes you can do it. Suppose you have a button view inside your block.xml u can access it in your java code, you need to give an id to your incude tag in that case: View firstBlock = findViewById( R.id.block1 ); // this is your inclide tag id View blockButton = firstBlock.findViewById( R.id.someButtonId ); // this is your button view id inside the block.xml – Zeba Jan 20 '15 at 08:57
0

You should inflate and add the sub layouts programmatically.

This link has example code: Android using layouts as a template for creating multiple layout instances

Community
  • 1
  • 1
Frank Harper
  • 3,027
  • 3
  • 30
  • 41