0

I'm storing my xml files in a hierarchical manner, subfolders were created based on the modules, to reduce cluttering of xml layout file in the folder. However, after refactoring, the R.id of each view is not detected from the Activity.java and tags in the layout xml is not detected by Android Studio.

I have added the src directory to my build.gradle and it still give me errors.

sourceSets {
    main {
        res {
            srcDirs 'src/main/res/layout/login_registration',
                    'src/main/res/layout/store_registration',
                    'src/main/res/layout',
                    'src/main/res'
        }
    }
}

Sample snapshot of the errors:

Can't determine type for tag '<FrameLayout android:id="@+id/login_registration_fragment_container" android:layout_height="match_parent" android:layout_width="match_parent" tools:layout_editor_absoluteX="171dp" tools:layout_editor_absoluteY="225dp"/>'

Can't determine type for tag '<include android:layout_height="match_parent" android:layout_width="match_parent" layout="@layout/app_bar_main"/>'

I have followed the guidance from Can the Android layout folder contain subfolders?. But it does not work. Am I doing something wrong or is there any alternative for managing layout xml files for large application.

EDIT: Thanks to the answer from https://stackoverflow.com/a/64905044/12311798. I have fixed the build issue. But the Android Studio still shows errors in both java and xml files. How can I get rid of the warning? Since the layout resources are detected by gradle.

Screenshot of errors in Android Studio:

Current Folder Structure

enter image description here

Updated src directory in build.gradle:

sourceSets {
        main {
            res {
                srcDirs  = ['src/main/res/layout/login_registration',
                        'src/main/res/layout/store_registration',
                        'src/main/res/layout',
                        'src/main/res']
            }
        }
    }
dylan salim
  • 420
  • 4
  • 6

2 Answers2

1

Correct format is

sourceSets {
    main {
        res.srcDirs =
        [
                'src/main/res/layout/login_registration',
                    'src/main/res/layout/store_registration',
                    'src/main/res/layout',
                    'src/main/res'
        ]
    }
}
Sarah Khan
  • 836
  • 7
  • 11
  • This solution does solve the build issue, now the application is build without errors. But how can I get rid of the warning in the java and xml files, currently the Android Studio still show the error in both of the files. Like the screenshot I provided in my question. – dylan salim Nov 19 '20 at 05:39
  • Can you try cleaning the code using Build -> Clean Project. If this doesn't work either, do File - > Invalidate Cache and Restart – Sarah Khan Nov 19 '20 at 05:42
  • I have tried to do both operations. The warnings still persist. I have restarted my IDE also, but it still show warnings – dylan salim Nov 19 '20 at 05:45
0

Even though the question was posted over 2 years ago, it is still very valid issue people might stumble into.

There is very important detail in project structure, which is easy to miss. When moving layouts into subfolders, each subfolder has to have "layout" folder like so:

-res
    -layouts
        -any_subfolder
            -layout
                some_layout_file.xml  
SariH
  • 71
  • 1
  • 2