Referencing another layout.xml file without duplicating it
Asked Answered
D

3

5

I need to provide the same layout.xml file for an Activity for several different qualifiers. I know that there's a way to just reference one existing layout.xml instead of really copying it and having a duplicate one.

But how? Can't find it in the Android docs right now... :-/

Anybody faster than me?

EDIT: although I found this "solution" I am still not there.

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <include layout="@layout/main_ltr"/>
</merge>

I need to point to a different qualifiers's layout file, not to another layout file in the same qualifier. Reason behind it: I specified the new Android 3.2 qualifier by proving screen width qualifiers. But on Android 3.0/3.1 this does not work, I need xlarge there, but I want it to be exactly the SAME file, not a copy!

Darwen answered 14/9, 2011 at 21:51 Comment(1)
C
9

If I understood correctly asker has one layout file for xlarge and sw-600dp and another one for all the rest. Anyway that was my situation when I stumbled on this question.

One can solve this by creating folders layout-xlarge and layout-s600dp and put one layout file in each but with the same contents. But one would like not to have two exact same files in two folders for obvious reasons.

This is my solution for the problem. Dont make layout-xlarge and layout-sw600dp files at all. Let's say you're creating a cool activity with layout file /res/layout/cool_activity.xml. Create your tablet layout file in the same folder but with a different name, i.e. /res/layout/cool_activity_for_tablet.xml and then create folders values-xlarge and values-sw600dp with layout.xml files in it with the following content

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="cool_activity" type="layout">@layout/cool_activity_for_tablet</item>
</resources>

Yes you will still have 2 of these with the same content but this is dumb content not the layout itself which can be hundreds of lines of xml.

Couplet answered 29/7, 2013 at 14:26 Comment(0)
H
4

<include> see Layout tricks #2

<merge> see Layout tricks #3

Hurty answered 14/9, 2011 at 22:10 Comment(3)
But how do I point to a layout in a DIFFERENT qualifier's directory, that is my problem! All of these includes and merges only got @layout and then the name, but not the directory, aka qualifier!Darwen
What do you mean by "qualifier"? Do you mean hdpi in e.g. drawable-hdpi?Hurty
Yes, that's a qualifier! The reason behind is: for Tablets with Android 3.0 and 3.1 I need a layout in layout-xlarge. BUT with Android 3.2 they introduced NEW qualifiers while the old one is discouraged: layout-sw600dp e.g. means at least 600 pixels width. Otherwise 7inch tablets won't get the tablet layout. Now, in my case BOTH layout.xmls need to be identical! How can I reference the one in xlarge in the sw600dp qualifier? Sometimes I really think Android is getting weird... :-/Darwen
S
0

Create a wrapper layout my_activity.xml and your layouts for small and xlarge devices.

<merge>
    <include layout="@layout/my_activity_small.xml"/>
</merge>

Your resources should look like:

layout
  -> my_activity.xml
  -> my_activity_small.xml
  -> my_activity_xlarge.xml

Now override my_activity.xml in layout-xlarge and layout-sw600dp:

<merge>
    <include layout="@layout/my_activity_xlarge.xml"/>
</merge>

Your ressources should look like:

layout
  -> my_activity.xml           <-- includes my_activity_small.xml
  -> my_activity_small.xml
  -> my_activity_xlarge.xml
layout-xlarge
  -> my_activity.xml           <-- includes my_activity_xlarge.xml
layout-sw600dp
  -> my_activity.xml           <-- includes my_activity_xlarge.xml

Use my_activity.xml in your code for loading the layout.

P.S.: You can not point to a layout in another qualifier's directory, as you mentioned in one of the comments.

Stylus answered 8/5, 2015 at 12:29 Comment(2)
Did you read my question to the end...? :-) I explicitly mentioned this as not being the correct answer... Sorry!Darwen
I explaned it step by step. If you are looking for the way how to reference layouts in directories with different qualifiers without duplicating them (as you mentioned in your question), here is the answer.Stylus

© 2022 - 2024 — McMap. All rights reserved.