Reputation: 20376
I have a LinearLayout defined in a file called intro_step1_activity.xml. I would like to load/reference this file in another xml file so that I do not need to retype the code in the other xml file. Is this possible and if so how can it be done?
Upvotes: 3
Views: 5907
Reputation: 433
first give an id
to that LinearLayout
and then define a LinearLayout
in java and reference it using the findViewById()
method.
then use the LinearLayout
object you just created to call the addView()
method.
if you want an example, comment.
Upvotes: 0
Reputation: 14199
Load an existing xml layout file in another xml layout file
to use xml file in other xml file use <include />
for your intro_step1_activity.xml
use code
<include layout="intro_step1_activity.xml"/>
Upvotes: 3
Reputation: 50538
Yes, it's possible using <merge>
or <include>
.
You can read more here and here
Upvotes: 0
Reputation: 157447
At runtime you can use an inflater, for instance:
Inflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.intro_step1_activity, null);
then you can add this view to the current view hierarchy. At compile time you can use the include tag for the xml
<include layout="@layout/intro_step1_activity"/>
Upvotes: 4