Home > Enterprise >  How to add a Layout from the resources to another Layout in Android?
How to add a Layout from the resources to another Layout in Android?

Time:08-17

I want to display a LinearLayout from the XML-resources in another pre-existing one so I can easily make copies from the same model.

(without having to declare programmatically all the buttons, textViews...)

I'm looking for a method like the one that works for activities:

setContentView(R.layout.myLayout);

Is it possible? And how?

Thanks a lot.

CodePudding user response:

Create a layout file sublayout.xml and put this file in

YOUR_APP/app/src/main/res/layout

then you can include it by

<include layout="@layout/sublayout"/>

in another layout file.

Anyway, according to my experience, you can not include sublayout.xml more than once, because each id, like android:id="@ id/sublayout_id1" can only be used once.

  • Related