0

StackOverflow Answers

I read that article and tried to make subfolders in app/src/main/res/layout/

I think I did the whole things which are mentioned in that answer.

In my gradle:

apply plugin: 
apply plugin: 
apply plugin:
apply plugin: 

android {
    // something
    sourceSets {
    main {
        res.srcDirs = getLayoutList("app/src/main/res/layout/")
    }
}
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
}

dependencies { ... }

My directory hierarchy model (Project)

Test
-- .gradle
-- .idea
-- app
  -- build
  -- libs
  -- src
    -- androidTest
    -- main
      -- assets
      -- java
      -- res
        -- layout
          -- about_login
          -- timetable
          -- activity_main.xml
      -- AndroidManifest.xml
    -- test
-- build
-- gradle
-- .gitignore
-- ... // Lot more setting files

But when I sync my gradle, the error came up and said, index error

def newPath = file.path.substring(throwAway.length() + 1)

at that line. Please help me.

Why this is different question?

1st. Many people works in their own PC but, I can't
2nd. I think that there has relationship between String.substring()
3rd. I cannot solve this problem.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
TyeolRik
  • 466
  • 2
  • 25
  • So, I was trying to change `throwAway.length() + 1` to `throwAway.length()` but, then, error appears. cannot find `R.id.~~` (But, in java, there is no compile error) – TyeolRik Jun 28 '17 at 08:49
  • Possible duplicate of [Can the Android Layout folder contain subfolders?](https://stackoverflow.com/questions/4930398/can-the-android-layout-folder-contain-subfolders) – RamaKrishnan Jun 28 '17 at 09:14

1 Answers1

3

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
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96