Organize XML’s layout in the Android project’s Resources(res)

Asked

Viewed 1,444 times

2

Problem

My project is growing and with it, the amount of files in the layout folder, and it is already becoming somewhat difficult to find a specific file, since they are not grouped in any way, only in alphabetical order.

So I tried to create subfolders for layout, to separate them by sessions, for example:

layout
-- layout_grid
   -- grid_detail.xml
   -- grid_item.xml
--layout_form
   -- form1.xml
   -- form2.xml

But this does not seem to be supported, by Android, since the xml's, are no longer found.

Questions

  1. I would like to know if it is possible somehow to create subfolders for layout?

  2. If the answer to the previous question is Not, then if there is any other good practice for organizing the layout folder?

3 answers

7


Unfortunately, really the answer is no (at first).

Android does not support subfolders in this case - accepts only files within predefined folders. What I have seen several times, is to name the files with a prefix, as if separated by the application packages, such as "usuario_", "venda_", etc...

However, it is possible to use subfolders with the help of Gradle, configuring it to recognize the subfolders you created. You can see an example here.

  • 1

    Very good, its indication, perhaps completed the 1° case of the prefixes, seems to be simpler, than the other, more being valid for future observation.

2

I organize like this:

  1. If the layout refers to a Activity, then the name will be activity_nome_da_classe
  2. If the layout refers to a Fragment, then the name will be fragment_nome_da_classe
  3. If the layout refers to an item from ListView, then the name will be list_item_nome_da_classe

And so on... I think I understand right? It’s just a suggestion.

2

There is actually a way to organize the Sources using subfolders.

In the file Gradle (app level), you will need to use the tag sourceSets:

sourceSets {
main {
   res.srcDirs = [
      'src/main/res',
      'src/main/res2'
   ]
}

Thus, the project will contain two 'Resources' folders', res and res2, and within each there will be the usual structure, with the folders drawable, layout, values and so on.

For a better example see this site: Organize folder Res.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.