why there are 2 drawable folder in res folder in android? - android

So I was pasting some image in the drawable folder and I found that there are 2 folders as you can see in below picture
one is drawable and another is drawable-v24
So my questions are
why there are 2 drawable folders ?
whats the diff. ?
Do we really need them ?
any additional information is deeply appreciated . :)

The drawable folder is the default folder. The drawable-v24 applies to only those devices with Android v24 or higher- any file in that directory will be used instead of the file with the same name in drawable on the applicable versions. Do you need them both? Only if you want different images on newer devices, or if there's some feature of v24 and higher that you want to take advantage of where available. If not you can safely delete it.

There are many drawable folder types based on many factors,
Android OS version, ex: drawable-V21, drawable-V22, drawable-v23...etc
Density pixels - Refer https://developer.android.com/training/multiscreen/screendensities
This different folder types are used to access the images for particular needs, say if you have to load a small size icon in Lollipop OS and a different size icon in all other OS, you have to use drawable-V21 for lollipop and normal drawable folder for all others.
Another case based on the resolution of mobile phones, quality will vary on images, example: drawable-hdpi images supports Full HD mobiles, whereas drawable-xxhdpi support QuadHD mobiles and so on..

Related

Image with different dimensions?

android studio 3.6
In android app I use one image with different dimensions. I put image in the next folders:
drawable-hdpi, drawable-mdpi, drawable-xhdpi, drawable-xxhdpi, drawable-xxxhdpi
Nice. It's work fine.
Now what about flutter app. Is I also need to create different folders for every image's dimension?
P.S. I need this image for android and iOS version
As mentioned in the Declaring resolution-aware image assets. You can create folders based on the resolution and add images to it.
The main asset is assumed to correspond to a resolution of 1.0. For
example, consider the following asset layout for an image named
my_icon.png:
.../my_icon.png
.../2.0x/my_icon.png
.../3.0x/my_icon.png
On devices with a device pixel ratio of 1.8, the asset
.../2.0x/my_icon.png would be chosen. For a device pixel ratio of 2.7,
the asset .../3.0x/my_icon.png would be chosen.
Reference Assets and Images
For an Android App created using Flutter, You can still use these folders as shown below for varying screen densities

App Crashes. Error Inflating XML. Cant find image because xxxhdpi folder is not there

Switched my eclipse project to Android studio. I was maintaining resources under drawable-mdpi folder only. Now in studio the preview of XML loads images correctly. However when I run the app in a device with resolution higher than mdpi the app crashes, shows error inflating binary XML.
After a long analysis I found the issue that the device was trying to load images from its corresponding density folder which is not available. So I created the folder drawable-xhdpi and put images in that folder. Now the app works fine.
Why android studio can't pick image from other density drawable folder and resize which is possible by eclipse. I can't maintain 5 different drawable folders because there are lots of images.
you have to add "drawable-hdpi" resource directory and paste all the hdpi resources there because currently 70% android devices supports hdpi resolution images.
if you only maintain the hdpi, then it is also ok.
android manages all remaining resouces from hdpi resouce directory.
Android application resource directories provide different layout designs for different screen sizes and different drawables. These different drawables are used by android to support a major range of all the android devices present out there. It's a standard practice to put your resources considering these densities. Coming back to your query:
Why android studio can't pick image from other density drawable folder and resize which is possible by eclipse. I can't maintain 5 different drawable folders because there are lots of images.
For your case,In order to maintain this you could create a drawable with nodpi and put your all resources there. nodpi focus resources for all densities.Your resources should be density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.
Hope this will clear your doubts, for more insight you can also look this.
you don't need to add all images to each difference size folder but depending the size of the image you might need to add images to different folders.
simple example is this can occour once you add high res/size images in normal drawble folder
Skipped 100 frames! The application may be doing too much work on its main thread.
This might not crash your app but will make it's performance down.
and
Different density folders were added later on for Android which means that...
If you wanted to be lazy and just add one asset the best choice would probably be the HDPI asset if your min app target < 8 and XHDPI if its >= 8. This is because the system will scale the resource up and down, but you would still want to start off with the highest resolution possible.
If you want to have complete control over how the assets are scaled then you can by all means provide your own for all / some of the densitys. In practise I generally provide HDPI / XHDPI as above and give all the resource buckets for things like logos / AB icons / App icons etc. I generally find the auto scaling to be pretty good and work for most situations, but will occasionally have to supply and extra LD/MD asset if its a small asset / contains small text etc. Plus if i duplicated all assets for things like XXXHDPI I would get pretty good apk bloat.
You can also use IDEs built in tools to add a single asset for many densitys at once. In Android Studio 0.6 this is File->New->Image Asset and a wizard will appear.
I have never noticed or heard of any perfomance impact of allowing Android to scale assets automatically - presumably this is done in hardware.
It may not look great when auto scaling down to LDPI say so you can optionally provide your own scaled assets for all other densities.
taken from : https://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
Do we need to add all images with different dpi to Android Apps

How to specify icons for different densities screen?

While working on an app, I noticed that there are different files such as drawable and drawable-hdpi. So I found out that these folders are for different icons and images size. My problem is that these two are two separate folders and I was wondering if I have to direct the android to the icons in the separate folder?
By that I mean do I have to first figure out what kind of dpi the device is and set the appropriate icons or can I just declare one drawable resource and android will figure it out for me?
If I can just set an icon name and the drawable file and the android system will know what to use for the different screen, then can you explain to me how this is done? I mean it is two separate files drawable and drawable-hdpi. So how can just set an icon image in the drawable folder and android knows what to access for the drawable-hdpi for the different device?
There are several questions bundled in one, but I will explain all of them in detail.
As you know, there are a variety of Android devices with different screen sizes. That is why, they are separated into a few categories based on their density. Must read this, if you haven't already.
This is the full classification,
So the idea is to have all these directories separately and not just a single "drawable" folder. Whenever you want to put any image or icon in your app, you need to put different sizes of them in their respective directories correctly.
two separate folders and I was wondering if I have to direct the
android to the icons in the separate folder?
You do not have to direct Android to do anything. Android will automatically pick the right image size from the "drawable-**dpi" intelligently.
By that I mean do I have to first figure out what kind of dpi the
device is and set the appropriate icons or can I just declare one
drawable resource and android will figure it out for me?
You can't figure out dpi of all devices, as your app will run on several devices having different densities. You just need to put different sizes of your icons in their respective drawable directory.
You should never put a single image or icon in the "drawable" directory as it can drastically slow down your app as Android need to dynamically resize your images at runtime.
If I can just set an icon name and the drawable file and the android
system will know what to use for the different screen, then can you
explain to me how this is done?
Just refer to the previous question, its already answered.
So how can just set an icon image in the drawable folder and android
knows what to access for the drawable-hdpi for the different device?
As explained earlier, Android obviously knows which device the app is currently running and thereby knows its screen density. Now it is very easy for Android just pick the right image based on the current screen density if you have put them properly in their respective drawable folders.
UPDATE
Just to answer your new question.
You start out with one "drawable" folder. But you need to create the other directories yourself and they need to have the proper name like drawable-mdpi, drawable-hdpi, drawable-xhdpi, etc.
You cannot have any other name like drawable-K or drawable-l, or not even a spelling mistake in the names. Android just scans to see if the folders with these names exists in your app, if so, then it will pick right one.
Suppose you are using a Nexus 5 which is an xxhdpi device,
i) Now Android will look for the drawable-xhdpi directory in your app for a particular image.
ii) If it exists, then it will just pick it up. If it doesn't then it will look for the nearest ones like drawable-xxhdpi or drawable-hdpi and resize the image slightly to fit.
iii) If nothing exists, then it will pick the image from the raw "drawable" folder.
Hope it clears all your doubts.

getDrawable from another resolution

Is it possible to get an image from res/drawable-xxxdpi if I use a ldpi device?
That is, R.drawable.sunmsg gives me the image link to current dpi.
I do not want to dublicate the resources from res/drawable-xxxdpi to res/drawable
Android prefers scaling down larger sized images than scaling up lower resolution images (quality is given more priority than size/performance).
So if you're using an mdpi device, and you've let's say an image in drawable-ldpi and drawable-xxxhdpi, the xxxhdpi will be preferred over ldpi, i.e. it'll first search for the image starting from mdpi to xxxhdpi, until it finds the image. If not found, it'll start going to lower resolutions and as a last resort visit the drawable folder.
So in your case, you basically don't need to do anything else. Just keep the image in drawable-xxxhdpi folder and let android handle the rest.
But this is not advisable, because scaling down the higher density image is equivalent to decoding a higher resolution image and then scaling it down, so it's going to be performance intensive, defeating the very purpose behind Android provisioning alternative configuration dependent resources.
Check Size and density specific resources
For a detailed documentation, check android's How Android Finds the Best-matching Resource
I have a open source project I have Hosted in github here.
.
Just run that project and that will tell you the device properties
of the device and form a proper folder structure targeting a
particular device
For Example I have a below folder structure for below device
properties.
With those structure you can target a particular device.
There are multiple combinations you can make setting up your
resource folder structure. - check here in android developers site
Check this link too.
.
drawable-sw360dp-normal-hdpi
Hope that helps !!

Multiple MDPI/HDPI drawables with different for same

I am new to android and this concept of multiple resources is killing me and its Halloween =)
Ok so for Normal screen we have following HDPI per http://developer.android.com/guide/practices/screens_support.html#range
WVGA800 (480x800)
WVGA854 (480x854)
600x1024
so my image will go in drawable-hdpi. should image be 480x800, 480x854, 600x1024, or all 3 in the drawable-hdpi?
If all 3 are doing to be in drawable-hdpi, how I will name them? They can't have same names.
Thank you in advance
The common drawable folders are drawable-hdpi,drawable-mdpi,drawable-ldpi.
These folders are used to tell android which set of image to use on different situations.
The different names that can be used are given here.
When a small ldpi phone is being used and it tries to refer an image named icon.png. It first refers the drawable-ldpi folder. If it doesn't find it there, it moves to the other folders till it finds the image. But if the phone was an hdpi device, android would first look into the drawable-hdpi folder. So if icon should be of different sizes, you put the different sized image in each folder with the same name. And android will decide which folder to access the image in run time.
This might be a bit confusing in the beginning but you will get used to it after a while.

Categories

Resources