Can the Android Layout Folder Contain Subfolders

Can the Android layout folder contain subfolders?

You CAN do this with gradle. I've made a demo project showing how.

The trick is to use gradle's ability to merge multiple resource folders, and set the res folder as well as the nested subfolders in the sourceSets block.

The quirk is that you can't declare a container resource folder before you declare that folder's child resource folders.

Below is the sourceSets block from the build.gradle file from the demo. Notice that the subfolders are declared first.

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

nested resources picture

Also, the direct parent of your actual resource files (pngs, xml layouts, etc..) does still need to correspond with the specification.

Creating sub folders within 'layout' res folder

try follow

sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/activity',
'src/main/res/layouts/fragment',
'src/main/res'
]
}
}

Your structure should like this:

--res 
--layouts
--activity
--layout
--main_activity.xml
--fragment
--layout
--main_fragment.xml

Android Organise Layouts into Sub Folders

I don't think you're going to be able to do this. I believe that it only supports certain folder names like layout-large,
layout-mdpi, layout-hdpi, layout-fr, etc. You can use more than one of these modifies as well like layout-fr-hdpi-large.

This page shows all of the allowed modifiers

http://developer.android.com/guide/topics/resources/providing-resources.html

Grouping layout by folders (Android)

You can follow these steps one by one:

  1. Create Directory in folder res called "layouts".
  2. Create Resource Folder in folder named layouts, lets say call it "main-activity"
  3. Create Directory in folder named activity called "layout"
  4. Move your files to this folder that makes most sense.
  5. Repeat for all your files.

At last add this to your build.gradle which declares where all the resources are located at:

android {
...
defaultConfig {
...
}
buildTypes {
...
}
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java', 'apt_generated']
aidl.srcDirs = ['src/main.aidl', 'apt_generated']
assets.srcDirs = ['src/main/assets']
res.srcDirs =
[
'src/main/res/layout/main-activity',
'src/main/res/layout/adapter',
'src/main/res'
]
}
}
}

Notice! src/main/res/layouts MUST be second last and src/main/res MUST be the last path

Also, switch to Project perspective to work.

In Android Studio, can layout xml files be grouped into folders?

No, the files have to be in predefined folders.

Here is a link to more information on available folder names:
http://developer.android.com/guide/topics/resources/providing-resources.html

sub folder for layout in android studio

Gradle won't create any directories for you. You have to create the following directory structure,

- res/layouts
- layout
- layout1.xml
- layouts_category2
- layout
- layout2.xml

Like this, we can use Gradle's ability to merge multiple resource folders. Note that it's not an Android feature.

Android studio src/res/layout subfolder error

There is a simple mistake in your project structure. you need to create a layouts directory NOT layout. So it should be:

app/src/main/res/layouts/

Then in your subfolder, you need create to create a layout directory. This should be where your layout file reside. For example, if you have login_activity.xml which you want to create inside subfolder about_login, you need to create the following directory:

app/src/main/res/layouts/about_login/layout

So, the login_activity.xml path will be:

app/src/main/res/layouts/about_login/layout/login_activity.xml

Then, in your app build.gradle, you should add the following:

sourceSets {
main {
res.srcDirs =
[
'src/main/res', // Try removing this
'src/main/res/layouts',
'src/main/res/layouts/about_login',
]
}
}

Each time you need to create a subfolder, you also need to create layout directory and add the XML layout file inside the layout directory.

Then your directory hierarcy should be like this:


Test
-- .gradle
-- .idea
-- app
-- build
-- libs
-- src
-- androidTest
-- main
-- assets
-- java
-- res
-- layouts
-- about_login
-- layout
-- login_activity.xml
-- activity_main.xml
-- AndroidManifest.xml
-- test
-- build
-- gradle

You can commenting your following code below, because we didn't need it:

def getLayoutList(path) {
File file = new File(path)
def throwAway = file.path.split("/")[0]
def newPath = file.path.substring(throwAway.length() + 1)
def array = file.list().collect {
"${newPath}/${it}"
}
def res="src/main/res";
array.push(res);
return array
}

Inside layout folder create multiple folder Android studio?

You can create a new folder inside the res directory. The new folder you create can be your res source.

Open app build.gradle file and add the following code:

sourceSets {
main {
res.srcDirs =
[
'src/main/res/layouts/activities',
'src/main/res/layouts/fragements',
'src/main/res/layouts/includes',
'src/main/res/layouts/items',
'src/main/res/layouts',
'src/main/res'
]
}
}

Repo link.

Sample Image



Related Topics



Leave a reply



Submit