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...
Actually
include
tags include all the elements in your root layout. So you can always access them usingfindViewById
on your Activity.Suppose you have a
alayout.xml
in which there is ainclude
layout. Now in one of your activity A,insideonCreate
you declaredsetContentView(R.layout.alayout)
Now inside your include layout you might have a button with idmyBtn
. You can access that insideonCreate
in the same way you could access it if it were in main layout:findViewById(R.id.myBtn)
You do know the id of the include. You can get that complete element, and then get one of its children starting from there.
All you need is the id of the parent view:
In this example, the
LinearLayout
is the parent of theTextView
I want from the included View.I can now do this:
There's a common pitfall with regard to resolving widget IDs when including the same XML layout multiple times. A blog post at http://www.coboltforge.com/2012/05/tech-stuff-layout/ explains exactly that problem and how to solve it!
i think your mean is NavigationView. you can access to inner layout by this way:
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: