Reputation: 6000
In the Android SDK documentation, all of the examples used with the @drawable/my_image xml syntax directly address images that are stored in the res/drawable directory in my project.
I am wondering if it is explicitly not okay to create a sub directory within the drawable directory.
For example, if I had the following directory layout:
res/drawable
-- sandwiches
-- tunaOnRye.png
-- hamAndSwiss.png
-- drinks
-- coldOne.png
-- hotTea.png
Could I reference the image of a tuna salad sandwich as @drawable/sandwiches/tunaOnRye
Or do I have to keep the hierarchy flat in the drawable directory.
Upvotes: 569
Views: 178025
Reputation: 1
I also wanted to do this but when I created a directory I didn't see it! after some second there was an error in app module...for fix it go to ThisPC, your android studio directory, your project,\app\src\main\res\drawable and delete it. it will be fix.
Upvotes: 0
Reputation: 411
I wanted to organize all my drawables into categories. This cannot be achieved using the regular directory structure, so my solution was to build the following directory structure:
- res
- drawable
- organised_res
- category1
- drawable
- category2
- drawable
- ...
- categoryn
- drawable
And then let gradle know about all the new resource directories I added: In build.gradle.kts, under android element I added:
sourceSets.getByName("main") {
res.srcDirs("src/main/organised_res/category1",
"src/main/organised_res/category2",
...
"src/main/organised_res/categoryn")
}
It's important to take into account that all drawable folders of all categories (and other resources) will be merged into a single non-hierarchical flat structure, so a pic.jpg in one of those dirs can be accessed from code simply as R.drawable.pic. Therefore it's important to make sure that all file names are unique.
Upvotes: 0
Reputation: 31
For anyone using Xamarin (either Xamarin.Android or Xamarin.Forms), there is a way to do this.
In the .csproj file for the Android project find the line for MonoAndroidResourcePrefix (documented, though rather poorly, here). Add the subdirectories you are wanting to use here, separating each entry by semicolons. When building, Visual Studio strips these prefixes so that all of the resources end up in a flat hierarchy. You may need to reload the solution after making these changes.
These directories do not need to be subdirectories of the default Resources directory in the project.
Make sure that files you add are getting the build action set to "AndroidResource".
For Xamarin.Android, the visual editor won't recognize images and will show the error "This resource URL cannot be resolved" but the project will build and the image will be visible at runtime.
Upvotes: 0
Reputation: 1163
It is possible to have multiple drawable folders by having an extra folder parallel to 'res' with a subdirectory 'drawable' and then add following to gradle:
sourceSets {
main {
res.srcDirs 'src/main/<extra_res>'
}
}
Tested with gradle 6.5.1
Upvotes: 0
Reputation: 116322
Actually, on Android Studio it is possible. You can have nested resources as shown here :
There is also a plugin to group resources here.
I recommend to avoid this though.
Upvotes: 45
Reputation: 1
Upvotes: -6
Reputation: 3445
There is a workaround for this situation: you can create a resVector
(for example) folder on the same level as default res
folder. There you can add any drawable-xxx
resource folders there:
resVector
-drawable
-layout
-color
After that all you need is to add
sourceSets {
main.res.srcDirs += 'src/main/resVector'
}
into your build.gradle
file (inside android { }
).
Upvotes: 6
Reputation: 3409
create a folder in main. like: 'res_notification_btn'
and create tree folder in. like 'drawable' or 'layout'
then in 'build.gradle' add this
sourceSets
{
main
{
res
{
srcDirs = ['src/main/res_notification_btn', 'src/main/res']
or
srcDir 'src/main/res_notification_btn'
}
}
}
Upvotes: 1
Reputation: 11
Subdirectories are not allowed, the resource must contain only [a-z0-9_.].
No you have uppercase letters, and no forward slashes.
Upvotes: 0
Reputation: 4410
Gradle with Android Studio could do it this way (link).
It's in the paragraph "Configuring the Structure"
sourceSets {
main {
java {
srcDir 'src/java'
}
resources {
srcDir 'src/resources'
}
}
}
Upvotes: 1
Reputation: 3645
In android studio with gradle you can have multiple source directors which will allow you to separate resources. For example:
android {
....
android.sourceSets {
main.res.srcDirs = ['src/main/extraresdirnamed_sandwiches', 'src/main/res']
}
....
}
However the names must not collide which means you will still need to have names such as sandwiches_tunaOnRye but you will be able to have a seperate section for all of your sandwiches.
This allows you to store your resources in different structures (useful for auto generated content such as actionbargenerator)
Upvotes: 9
Reputation: 77
assets/ You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data. http://developer.android.com/tools/projects/index.html
Upvotes: 0
Reputation: 1159
Check Bash Flatten Folder script that converts folder hierarchy to a single folder
Upvotes: 0
Reputation: 700
Not mine but I found this thread when looking for this issue, if your using Android Studio and Gradle Build system its pretty easy no plugins necessary just a little build file editing
https://stackoverflow.com/a/22426467/618419
Upvotes: 1
Reputation: 10245
I've wrote an eclipse plugin which allows to create virtual subfolder by separating the file name with two underscores __
. The project is in early stages, but don't worry it won't crash your IDE
more details can be found here, feel free to fork and send pull requests:
https://github.com/kirill578/Android-Sorted-Res-Folder
Upvotes: 22
Reputation: 11
This is not perfect methods. You have to implement same way which is display here.
You can also call the image under the folder through the code you can use
Resources res = getResources();
Drawable shape = res. getDrawable(R.drawable.gradient_box);
TextView tv = (TextView)findViewByID(R.id.textview);
tv.setBackground(shape);
Upvotes: 1
Reputation: 805
#!/usr/bin/env ruby
# current dir should be drawable-hdpi/ etc
# nuke all symlinks
Dir.foreach('.') {|f|
File.delete(f) if File.symlink?(f)
}
# symlink all resources renaming with underscores
Dir.glob("**/*.png") {|f|
system "ln -s #{f} #{f.gsub('/', '_')}" if f.include?("/")
}
Upvotes: 0
Reputation: 131
With the advent of library system, creating a library per big set of assets could be a solution.
It is still problematic as one must avoid using the same names within all the assets but using a prefix scheme per library should help with that.
It's not as simple as being able to create folders but that helps keeping things sane...
Upvotes: 3
Reputation: 4053
I like to use a simple script to flatten an organized directory structure provided by designers to something that can be used to generate an R file.
Run with current path in drawable-hdpi:
#! /bin/bash
DIRS=`find * -type d`
for dir in ${DIRS} ; do
for file in `ls ${dir}` ; do
mv ${dir}/${file} ${dir}_${file};
done
rmdir ${dir};
done
Upvotes: 9
Reputation: 265
Use assets folder.
sample code:
InputStream is = null;
try {
is = this.getResources().getAssets().open("test/sample.png");
} catch (IOException e) {
;
}
image = BitmapFactory.decodeStream(is);
Upvotes: 24
Reputation: 4985
The workaround I'm using (and the one Android itself seems to favor) is to essentially substitute an underscore for a forward slash, so your structure would look something like this:
sandwich_tunaOnRye.png
sandwich_hamAndSwiss.png
drink_coldOne.png
drink_hotTea.png
The approach requires you to be meticulous in your naming and doesn't make it much easier to wrangle the files themselves (if you decided that drinks and sandwiches should really all be "food
", you'd have to do a mass rename rather than simply moving them to the directory); but your programming logic's complexity doesn't suffer too badly compared to the folder structure equivalent.
This situation sucks indeed. Android is a mixed bag of wonderful and terrible design decisions. We can only hope for the latter portion to get weeded out with all due haste :)
Upvotes: 167
Reputation: 1644
One way to partially get around the problem is to use the API Level suffix. I use res/layout-v1, res/layout-v2 etc to hold multiple sub projects in the same apk. This mechanism can be used for all resource types.
Obviously, this can only be used if you are targeting API levels above the res/layout-v? you are using.
Also, watch out for the bug in Android 1.5 and 1.6. See Andoroid documentation about the API Level suffix.
Upvotes: 6
Reputation: 3109
Yes - it does suck :) However you can use the assets folder and have sub directories in there and load images that way.
Upvotes: 40
Reputation: 96916
No, the resources mechanism doesn't support subfolders in the drawable directory, so yes - you need to keep that hierarchy flat.
The directory layout you showed would result in none of the images being available.
From my own experiments it seems that having a subfolder with any items in it, within the res/drawable
folder, will cause the resource compiler to fail -- preventing the R.java
file from being generated correctly.
Upvotes: 597