Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing another layout.xml file without duplicating it

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!

like image 309
Zordid Avatar asked Sep 14 '11 21:09

Zordid


People also ask

Can you use a same XML layout for 2 different activities?

Yes, you can. Just call "setContentView" with the same xml and it will use the same layout. Change whatever you need to programmatically during runtime.

Is it possible to include one layout definition in another?

Reuse layouts with <include/> To efficiently reuse complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts.

In which directory XML layouts are stored?

Specifically, Android considers XML-based layouts to be resources, and as such, layout files are stored in the reslayout directory inside your Android project. Each XML file contains a tree of elements specifying a layout of widgets and containers that make up one View.


1 Answers

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.

like image 100
Nemanja Kovacevic Avatar answered Nov 15 '22 05:11

Nemanja Kovacevic