RunLoop
RunLoop

Reputation: 20376

Include an existing XML layout file in another XML layout file

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

Answers (4)

kayveesin
kayveesin

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

Tarsem Singh
Tarsem Singh

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

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

Yes, it's possible using <merge> or <include>.

You can read more here and here

Upvotes: 0

Blackbelt
Blackbelt

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

Related Questions