3

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?

4 Answers 4

4

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"/>
Sign up to request clarification or add additional context in comments.

1 Comment

From Android documentation: It is never used directly. Instead, use getLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example: LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
3
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"/>

1 Comment

<include layout="@layout/my_layout_file"/> seems to work also
0

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

You can read more here and here

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.