How do the drawable folders work nowadays?
I used to be able to just add folders named drawable-mdpi and such to get image resources, but this feature has seemingly disappeared. Image assets doesn't allow me to add image assets that aren't icons without messing them up and manually adding the drawable folders I need doesn't work any more as android can no longer find the resources.
How does android look for drawables now and how can I add prescaled images to my projects manually?
Related
I am following the tutorials in the Big Nerd Ranch book for android but that book is structured for Eclipse + ADT.
I am trying to add an arrow icon to a button and I have folders (drawable-hdpi, drawable-mdpi, drawable-xhdpi) that contain sets of identical images. Ideally I would like these folders to appear in the drawable folder but whether I copy and paste or drag these folders it doesn't seem to work. They appear in the packages section instead, but when this happens I can't seem to reference them from xml files in the layout folder.
The best I can do is drag/copy the files (not the folder)into the drawable folder, in which case they stay there and I am able to use them. The problem with this is I can have, at most, one pixel density for the set of images that is in there. The only way I can think of is if I change the names of all of them so that the problem of overwriting is gone.
However in the mipmap folder in the ic_laucher.png folder there are four files of the same name, except there are greyed out density descriptions beside them. How is this done?
What should I do?
As you can see I have now added the images using the file system. I am still unable to reference these using the resource name however. I am still not sure why.
What you are seeing in the mipmap "folder" is just some UI magic. The Android project view in Android studio will combine all resources with the same name into a single entry like you are seeing for ic_launcher.png to make it easier to view your resources.
Behind the scenes, this still uses different folders on disk. If you open up your project in Windows Explorer, you will see separate folders for mipmap-xhdpi, mipmap-mdpi, etc.
The same is true for your drawable folders. You still want to keep your images in separate density-specific folders on disk. Android Studio will then display them to you as if they were all in the same drawable folder in the Android project view. You cannot physically move your density specific folders beneath the drawable folder.
I have my image resources in the various mipmap folders (mipmap-xhdpi, mipmap-xxhdpi, etc.) My project has hundreds of images (and growing) and it is becoming cumbersome that they are all in a single level in each directory.
I want to create a subfolder, such as "sprites" that I can reference in code with something like
R.mipmap.sprites.sprite_name
How can I create usable sub-folders in my mipmap folders?
When I create a new directory inside of a mipmap folder (using New -> Directory), the directory and its contents are invisible to anywhere that I am specifying a resource, namely:
• In the design-view of my xml layout, when I am browsing for an image resource for a background or an image button, the sub folders are hidden.
• In code, when I am specifying an image resource (R.... the subfolders are ignored).
Is there a correct way to do this? There appear to be some other directory options, Under the New -> Folder category, I have:
• Assets folder, described as "create a source root for assets to be included in the apk," but I don't want a root, I want a subdirectory.
• Res Folder, "source root for android resource files" 1. I don't see how this is different than an asset and 2. again, I don't want a root.
There are also several other folder types listed, that I am not familiar with.
If this cannot be accomplished, what is the correct way to include (and organize) hundreds (or thousands) of image resources in a project? Is the only way to do it really to have them all in a single directory?
How can I create usable sub-folders in my mipmap folders?
You can't. Resource directories of any time, mipmap or otherwise, cannot have subdirectories.
what is the correct way to include (and organize) hundreds (or thousands) of image resources in a project? Is the only way to do it really to have them all in a single directory?
If you are going to go with resources, then yes, though I would put them in drawable directories. mipmap is mostly for launcher icons.
the mipmaps are special because the app will display images of a different resolution depending on the device resolution
No. mipmap and drawable resources support resource resampling at runtime, but it is based on density, not resolution.
As ByteHamster notes, you can put whatever sort of directory structure you want in assets/, though you will need to put the appropriate values into BitmapFactory.Options to help with the resampling based on density.
Yes, you must keep all the resources in a single folder because, as you may know, different folders are used to target different device classes.
The right way to organise your resources in by prefix: as stated in the official Android documentation, you should use prefixes to make the resources appear in a meaningful order.
I recently installed one more Android SDK to Android Studio, the SDK version 4.4 (API level 19), and after using it with my project, it added a set of mipmap folders in the project's res folder (res/mipmap).
I neither understand why, or how to use them.
I read another question about it here on SO.
The answer to it said that:
The mipmap folders are for placing your app icons in only. Any other
drawable assets you use should be placed in the relevant drawable
folders as before.
According to this Google blogpost:
It’s best practice to place your app icons in mipmap- folders (not the drawable- folders) because they are used at resolutions
different from the device’s current density.
I still don't get the reason to this.
Why cant we just place the app icons in the res/drawable folders too?
Also, if only the app icon should be placed in the mipmap folders, how do I create other drawable assets and place them in the drawable folders with the Android Studio Image Asset wizard? (Accessed by right clicking in the project, selecting New > Image Asset)
Why cant we just place the app icons in the res/drawable folders too?
Oh, you can. Google doesn't recommend it, that's all.
Quoting myself from Version 6.5 of my book:
While
drawable resources might be removed when packaging an APK, such as for the
Gradle for Android split system for making density-specific editions of an app,
mipmap resources are left alone, apparently
This is because launcher icons may be pulled from a higher-density bucket than the regular device density that is used for all other drawables. Putting them in res/mipmap-*/ directories means that we won't inadvertently get rid of them as part of optimizing APK size.
(why they didn't handle this in a better way, I have no idea)
how do I create other drawable assets and place them in the drawable folders with the Android Studio Image Asset wizard? (Accessed by right clicking in the project, selecting New > Image Asset)
The Image Asset wizard only puts launcher icons in res/mipmap-*/ directories. If you choose the other icon types, they will go in res/drawable-*/. And, of course, the Image Asset wizard itself is optional.
I am creating an app using android studio.
i created the assets folder and created sub directories under it for each activity i am using (each one has a different set of images).
But on the other hand i want to support multiply screen resolutions which as i read is supported using drawable folder's, also drawable doesn't support sub directories.
I am using API 19 at the moment.
Putting all the images in drawable folder will be a bug mass.
How can i achieve my goal of order in my images and supporting multiple screen resolutions
Thank you in advance
there are subfoldes in drawable. take a look at this http://developer.android.com/guide/practices/screens_support.html
The drawables folders allow you to save various resolution versions of an image into separate folders. The OS will choose the right asset based on the device screen density for you when you ask for #drawables/image_name. /assets is more like a normal file directory. In general if you pull from assets you need to handle resolution differences on your own.
When i run my app on android 2.3 emulator, without creating the /drawable directory, images are loaded from the drawable-hdpi directory.
But when i create the /drawable directory, images are loaded from the drawable instead of the drawable-hdpi directory. I have create the drawable folder to make my app to run also on android 1.5.
What should i do to load images from drawable-hdpi/mdpi/ldpi on android >=1.6?
The resources that are loaded are specific to the target that the application is being run on. If the phone is an HDPI qualified phone, then it will load those resources.. if its MDPI then it will load those resources...
You shouldn't be setting these directly, because this is handle specifically by the frameworks.
The rules used by Android to find the best matching resources at runtime are explained in this document.
Especially: