views:

151

answers:

2

I understand how to create a custom Android control, and I believe I understand how to pull attributes for it from the Xml layout. I don't know, however, how to get any children elements from it.

Here's a visual example of what I'm trying to accomplish:

public class Menu extends LinearLayout
{
    // Custom Code
}

public class MenuItem extends Button
{
    // Custom Code
}

<!-- Layout -->
<?xml version="1.0" encoding="utf-8"?>
<Menu>
    <MenuItem/>
    <MenuItem/>
</Menu>

When my Menu is created, how do I get references to the two MenuItems?

+1  A: 

You should use an android:id to do that. If you will not know how many childs you will have:

LinearLayout extends ViewGroup, so you can use getChildAt() and getChildCount() to get those views.

Macarse
I'll now how many children are in the Menu because I'll add them to the Xml, but I want this to be dynamic enough that I don't need to know. I'll give getChildCount() a try. Thanks!
Quenton Jones
A: 

for accesing any control (custom or system ) an id is a must. by specifying an id you give it a unique identity. Using this id you can get a reference to that control.

Umesh