Aliasing an entire resource folder with qualifiers - android

My app includes drawable resources for mdpi through xxhdpi. It's mainly intended for tablets, but it may occasionally be used on phones. The problem is that my drawables take up too much screen real estate on xhdpi phones.
I'd like to set up aliases so that xhdpi-small devices use the hdpi or mdpi drawables. I read the docs, but the examples there are pretty limited. Is there a way to alias specific resources with qualifiers, as opposed to resources in the unqualified drawable folder? And is there a way to alias the contents of an entire drawable folder, or do I have to alias each drawable individually?
Edit: I understand that I need to create a qualified resource folder. The question is about how to alias the resources in that folder to refer to resources in another qualified folder.

Make sure that you have put drawables in each folder with proper scaling ratio(3:4:6:8:12:16).If you still have problem on small screen size then provide separate layout for small and large screen sizes.

Related

Folders for different screen densities for Android

There are multiple groups of screen densities of Android phones (ldpi, mdpi, hdpi and so on).
I had a folder "drawable" with picture size 1920 x 930 pixels which is for xxxhdpi, I guess.
I want to arrange pictures for all screen densities from xxxhdpi to hdpi. So, I should rename my existing folder: "drawable" -> "drawable-xxxhdpi". Then, I should create 3 additional folders for others. Do I understand it right?
How would actually the program choose proper folder?
For example, I also have smaller pictures now. Should I also resize them? If I don't duplicate them in new folder, will the program crash?
Will the program crash on phones with, for example, hdpi if I don't create a folder for hdpi?
Couldn't get how it works, would be grateful for an explanation.
First of all be sure that you are in the "Project" view because you will not see all the folders if you are in "Android" view. Then instead of renaming drawable folder create the others and keep drawable for some files that doesn't need to resize (for example defining a background). Folders should be:
drawable-hdpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
Selection between them will be done automatically according to the screen of the device you are installing the app. If screen is xxxhdpi and you don't have a file for that resolution the app will take the smaller and so on. Obviously this will not be a great UI performance but you should not have problems with crashes.
There are many online App icons generators in the web so you can take a big icon and put it there so you get the icons for all sizes

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

Avoid duplicate files in different android drawable folders

I'm developing an android game and will try cover as many different phone and tabled screen sizes and resolutions as possible. To make it easier to handle graphics I've designed some of my icons so that for example a 7" mdpi tablet uses same icons as an xhdpi phone.
That means I have put exactly the same icon file in drawable-xhdpi folder as in drawable-sw600dp-mdpi. I also have same files in drawable-land-xhdpi as in drawable-sw600dp-land-mdpi. It doesn't feel good to have duplicate files, can it be avoided somehow? Or how smart is eclipse compiler, can it detect duplicate files and compress the apk file accordingly?
You can avoid duplicating image files by using aliases. An image alias is an XML resource file containing a <bitmap /> tag. Look at the documentation for the syntax.
In your case I think you should name your resources differently for phone and tablets if they don't end up having the same density-independent size. From what I understand, your tablet images are bigger than your phone images, which is generally not a good idea.

Res folder and dpi?

I just want to clarify, with the res folders in android, with lower resolution devices, they automatically get the same named asset from the ldpi folder? This is so I can make different assets for smaller screens?
Do I need to have assets in all 4 folders (ldpi - xhdpi) for it to work on all sized devices? Or can I just have assets in one folder if the assets are all the same for all devices?
And what if my assets are only different for low res devices, do I need to mirror the contents of each of the other folders so they all have the same assets in?
I would suggest reading this article thoroughly to get an understanding of exactly how it works, but basically, it will find the closest matching size resource that is available, so no, you don't need to mirror across all folders necessarily.
From the qualifiers section particularly, this quote explains it well.
Be aware that, when the Android system picks which resources to use at
runtime, it uses certain logic to determing the "best matching"
resources. That is, the qualifiers you use don't have to exactly match
the current screen configuration in all cases in order for the system
to use them. Specifically, when selecting resources based on the size
qualifiers, the system will use resources designed for a screen
smaller than the current screen if there are no resources that better
match (for example, a large-size screen will use normal-size screen
resources if necessary). However, if the only available resources are
larger than the current screen, the system will not use them and your
application will crash if no other resources match the device
configuration (for example, if all layout resources are tagged with
the xlarge qualifier, but the device is a normal-size screen). For
more information about how the system selects resources, read How
Android Finds the Best-matching Resource.
Lets break your question into three seperate questions.
I just want to clarify, with the res folders in android, with lower
resolution devices, they automatically get the same named asset from
the ldpi folder? This is so I can make different assets for smaller
screens?
Quite simply, yes. That's what the system is for. As long as you use assets with the same name, Android will automatically pick the one that works best for your device, depending on the dpi of the screen.
Do I need to have assets in all 4 folders (ldpi - xhdpi) for it to
work on all sized devices? Or can I just have assets in one folder if
the assets are all the same for all devices?
You do not need the assets in all folders to have it work on all devices. However, if you want it work well, you will need assets in all folders, all with their own dimensions depending on the folder. If you only use one folder with one size of assets, it will still work. Your assets will just get scaled by Android itself which can cause some assets to look really ugly on some higher/lower dpi devices. Let alone the fact that the scaling can cause your entire layout to mess up.
And what if my assets are only different for low res devices, do I
need to mirror the contents of each of the other folders so they all
have the same assets in?
By "only different for low res devices", do you mean that the entire asset is different? For instance a blue image instead of a red one? If so you would still need to use the same name as the assets in the other folders. The actual content can be different, the OS only checks the names when grabbing the assets.
For more info about how to support multiple screen sizes, give this guide a try:
http://developer.android.com/guide/practices/screens_support.html

Android - Cascading drawable folders and default drawables

I have the following drawable folders
/drawable/
/drawable-hdpi/
/drawable-ldpi/
/drawable-mdpi/
/drawable-xhdpi/
If I have an image resource, do I need to create 5 (or 4?) different resolution versions of the file? Let's say I make just two versions and put them in their respective folders:
/drawable/
/drawable-hdpi/image.png
/drawable-ldpi/
/drawable-mdpi/image.png
/drawable-xhdpi/
What happens when someone on a LDPI or XHDPI device visits an Activity that needs to display image.png? Does it just not show it? Does the app crash? Or does Android follow a resource cascading system, show the version that is nearest to the current screen density?
Also, in this case I don't have an image.png in the /drawable/ folder. Is this bad? Should every single resource have (at minimum) a version in the /drawable/ folder?
Also, if you have specified all 4 screen density drawable folders (xhdpi, hdpi, mdpi, ldpi), then what is the point of the normal /drawable/ folder? When will it ever get used?
If I have an image resource, do I need to create 5 (or 4?) different
resolution versions of the file?
No, you don't have to
What happens when someone on a LDPI or XHDPI device visits an Activity
that needs to display image.png? Does it just not show it? Does the
app crash? Or does Android follow a resource cascading system, show
the version that is nearest to the current screen density?
Android uses a nice cascading system for its resources. This page explains it a bit. Afaik LDPI devices use the MDPI resource if a LDPI resource isn't available, XHDPI uses HDPI resources. (I could be a bit off, I can't recall the exact rules) Your app doesn't crash.
Your app crashes if you only have higher resolution resources than the screen can take.
Also, in this case I don't have an image.png in the /drawable/ folder.
Is this bad? Should every single resource have (at minimum) a version
in the /drawable/ folder?
No, not necessary.
Also, if you have specified all 4 screen density drawable folders
(xhdpi, hdpi, mdpi, ldpi), then what is the point of the normal
/drawable/ folder? When will it ever get used?
The drawable folder is for images that don't need scaling, and things like shape drawable resource XML files etc.

Categories

Resources