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
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"/>
1 Comment
LightMan
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);
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
O-9
<include layout="@layout/my_layout_file"/> seems to work also