Multiple instances of included layout, namespacing question.

850 views
Skip to first unread message

free1000

unread,
Jan 12, 2010, 2:09:15 PM1/12/10
to Android Developers
I've been attempting to use the <include> tag in some layouts and it
seems that these are of limited use because of the flat namespace of
the R.id.x approach.

Suppose I have a layout (attribs and xml namespace decl omitted for
clarity).

component.xml

<LinearLayout android:id="@+id/row" >
<ImageView android:id="@+id/image" />
<TextView android:id="@+id/text" />
</LinearLayout>

Now include this twice inside a 'parent' view

flipper.xml

<ViewFlipper android:id="@+id/flipper" >
<include android:id="@+id/row1" android:layout="component" />
<include android:id="@+id/row2" android:layout="component" />
</ViewFlipper>

Now in my java code I have a conundrum. I am not able to identify each
individual instance o f the children included layouts root view. In
the include tags I can replace the root id with "row1" and "row2" so
I can find the individual linear layouts using findViewById

eg:

LinearLayout row1 = (LinearLayout) findViewById(R.id.row1)
LinearLayout row2 = (LinearLayout) findViewById(R.id.row2)

However, I can't easily access the two instances of the TextView which
are children of row1 and row2 because all I have to play with is the
single id "text"

ie:

TextView text = (TextView) findViewById(R.id.text);

In this case, which instance (row1.text or row2.text) does this refer
to, both of them, or none?

I guess I would need to use another means of accessing the children of
row1 and row2.

Anyone know how this can be done?


Romain Guy

unread,
Jan 12, 2010, 4:30:29 PM1/12/10
to android-d...@googlegroups.com
It's easy:

LinearLayout row1 = (LinearLayout) findViewById(R.id.row1)

TextView text1 = row1.findViewById(R.id.text);


LinearLayout row2 = (LinearLayout) findViewById(R.id.row2)

TextView text2 = row2.findViewById(R.id.text);

> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-d...@googlegroups.com
> To unsubscribe from this group, send email to
> android-develop...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

--
Romain Guy
Android framework engineer
roma...@android.com

Note: please don't send private questions to me, as I don't have time
to provide private support. All such questions should be posted on
public forums, where I and others can see and answer them

free1000

unread,
Jan 12, 2010, 5:30:20 PM1/12/10
to Android Developers
Thanks Romain. I should have read the API docs rather than just the
dev guide, its pretty obvious from the docs for findViewById now that
I look at it.

> romain...@android.com

Reply all
Reply to author
Forward
Message has been deleted
0 new messages