How to access Button inside "include" layout
Solution 1:
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
Solution 2:
All you need is the id of the parent view:
In this example, the LinearLayout
is the parent of the TextView
I want from the included View.
I can now do this:
View view = findViewById(R.id.include_title_layout);
TextView titleTV = (TextView) view.findViewById(R.id.newsTitleTextView);
titleTV.setText("Whatever");
Solution 3:
You do know the id of the include. You can get that complete element, and then get one of its children starting from there.
Solution 4:
Actually include
tags include all the elements in your root layout. So you can always access them using findViewById
on your Activity.
Suppose you have a alayout.xml
in which there is a include
layout. Now in one of your activity A,inside onCreate
you declared setContentView(R.layout.alayout)
Now inside your include layout you might have a button with id myBtn
. You can access that inside onCreate
in the same way you could access it if it were in main layout: findViewById(R.id.myBtn)