import xml into another xml
You can use
<include layout="@layout/commonlayout" android:id="@+id/id" />
commonlayout.xml
should be defined in res/layout
where you can add the repeated parts.
As Labeeb P rightly said, it works. Just want to add that you can also override parameters too:
<include
layout="@layout/commonlayout"
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="2sp"
android:layout_marginRight="2sp"
/>
In addition to those great answers, you can also avoid code duplication by using the <merge>
tag, like so:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</merge>
The <merge>
part gets stripped when you include it into other xml. This might help including more than a single Button
at a time. See the official documentation.