Stefano Cazzola
Stefano Cazzola

Reputation: 1687

Android layout - Hierarchical definition

I'm very new to Android development, so I may be asking something really stupid and the answer might simply be 'no'. But can't find a sound answer anywhere.

I know that you can include some "snippets" in an Android layout with the tags include or merge, but what I'm really looking for is if it is possible to do that the other way around.

Is it possible to define Android layout in a MVC style? That is, definining a base layout and then customize inner parts of it, a bit like you do in a Java MVC web app?

Required compatibility at least with KitKat (Jelly Bean would be better).

EDIT - Pseudo code requested

Actually, I can't imagine how a code could look like in Android to do that.

I try to describe it graphically.

What I'm able to do so far is including or merging:

LAYOUT <-- this is the layout specific for my activity
|     -----------
|-----| SNIPPET |  <-- generic snippets which can be reused
      -----------

which means that my layout actively includes a snippet (for instance, a progress bar, or the typical progress circle)

What I'd like to be able to achieve is the opposite way:

---------------
| ROOT LAYOUT |  <-- generic layout defined for ALL activities
---------------
|
|---- CUSTOMIZATION  <-- this is defined for each specific activity.

A classic example in web development, which I'm more used to, is the definition once and for header and footer in a basic layout file. Then each web page (or JSP(X)) will redefine the content of the page deriving from the root layout, thus inheriting (a bit in OOP flavour) the common parts defined in the root layout.

Hope this makes it clearer.

Upvotes: 0

Views: 31

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You seem to look for <include> tag. See docs on this subject here: Re-using Layouts with <include/>. You can also consider using Fragments yet, it is not an equivalent to <include> and sometimes may be an overkill for what you want to acheive.

Upvotes: 2

Related Questions