guest86
guest86

Reputation: 2956

Android layout reuse

I have an Android application that needs to use certain linear layout in several activities. For that reason, i have extracted needed linearlayout into separate xml file but now i don't know how to add that layout to other layouts. Simply, my idea is this:

<xml layout id: "SomeSharedControls" />

<xml layout id:"mainWindow">
add @id SomeSharedControls
...
other xml controls in current window
...
</xml>

<xml layout id: "anotherWindow">
add @id SomeSharedControls
...
other xml controls in current window
...
</xml>

How to achieve that?

Upvotes: 1

Views: 194

Answers (3)

Yuriy Kulikov
Yuriy Kulikov

Reputation: 2129

If your application targets Android 4+, you should use fragments. Fragments are great for UI reuse and do exactly what you want. Fragment is a self-contained piece of code with its own lifecycle (just like an Activity), which you can use from your activity almost just like any other View. Check http://developer.android.com/guide/components/fragments.html

Upvotes: 0

blackcj
blackcj

Reputation: 3661

<include
    android:id="@+id/shared_view"
    layout="@layout/shared_view"/>

Where shared_view.xml is the file with your re-usable code.

Upvotes: 1

Ahmad
Ahmad

Reputation: 72533

You can use <include>. Read this.

Upvotes: 1

Related Questions