How to access Button inside "include" layout

chow picture chow · Jan 24, 2011 · Viewed 43.5k times · Source

Refer to the doc: http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html

I have a button inside the included layout, how can I access the button? I don't know the id! How can I define the OnClickListener...?

Please help...

Answer

Jems picture Jems · Jan 24, 2011

The id you have with the include tag is assigned to the root View of the included layout. First get a reference to that View using findViewByid. Then you can call findViewById on that specific View to get a reference to a View inside the layout. So:

View myLayout = findViewById( R.id.cell1 ); // root View id from that link
View myView = myLayout.findViewById( R.id.someinnerview ); // id of a view contained in the included file