0

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.

Bat BRT
  • 47
  • 2
  • 6

1 Answers1

2

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.

sidcoder
  • 460
  • 2
  • 6
  • Thanks! But I have another question ;) Is it possible to include the layout programmatically ? – Bat BRT Aug 17 '22 at 07:05
  • 1
    Have a look here: [add Layout programmatically inside another one](https://stackoverflow.com/questions/28473145/add-layout-programmatically-inside-another-one) – sidcoder Aug 17 '22 at 07:09
  • Why do you need this. I never needed this. You can set the visibility of some layout to "gone" by `layout1.setVisibility(View.GONE);` in the beginning, and when you need it, then `layout1.setVisibility(View.VISIBLE);`. This has a similar effect like adding `layout1` programmatically. Anyway, it is simple, because you can just define `layout1` in the xml-file. – sidcoder Aug 17 '22 at 07:16
  • 1
    I need this because I add multiple elements from a database into the Activity. So I don't know how many of them I have to display before launching the activity... Anyway, the link you gave me seem to answer my question ;) Thank you for this! In fact I'm pretty new to Android, so I'm not yet used to the inflaters and so on... – Bat BRT Aug 17 '22 at 07:26
  • 1
    Just in case, you are looking for a work-around (like I did it). I limited the maximum number of options (from server data) to 20. Then, you can do the "trick" with using visibility = gone (and if options > 20, then use button, to show next ... ). – sidcoder Aug 17 '22 at 07:40
  • Another work-around: Add all data to a String in a text field (in a scroll view) in separate lines and include a link for each option (instead of a button). This is a very quick solution (for prototyping or for production, if the formatting is nice). Some keywords: `SpannableStringBuilder` and `str1.setSpan(new ClickableSpan() {@Override public void onClick(View widget) { some action } }, 0, str1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);` – sidcoder Aug 17 '22 at 08:09
  • In fact I'm already adding all my layouts to a scrollView ;) At first I was only adding text as you say but, with all the lines I have, it became really ugly... Even if was trying (without succeeding at all) to format the lines... That is why using a single model of Layout repeted as much time as needed seemed better to me. But the idea of replacing buttons by links is nice! Thanks again! – Bat BRT Aug 17 '22 at 08:21
  • OK. For the link in the `TextView tv` to work, you also need `tv.setMovementMethod(LinkMovementMethod.getInstance());` – sidcoder Aug 17 '22 at 08:27
  • Or use HTML for formatting just by: `tv_message.setText(Html.fromHtml(String.valueOf(ssb)));` `tv_message.setMovementMethod(LinkMovementMethod.getInstance());` – sidcoder Aug 17 '22 at 08:37