Android resources "best match" fallback logic - android

I've read the docs about Android Resources and I think I've understood the best-matching logic used by android to identify in which directory a particular resource should be searched first.. Supposing that drawable-hdpi, drawable-en-port and drawable-en* match the current device configuration, drawable-en-port is the best matching directory.. My question is, if a drawable is not found in drawable-en-port, does the system look directly in drawable or does it look in the second-best-match drawable-en and then in the third best match drawable-hdpi and so on until it reaches drawable? I suppose it works this way, but I did not find it explicitly said in the docs (unless I've read them too quickly and you will surely kill me :) ).

Yes it looks first in the drawable-en, then drawable-hdpi because language qualifier has higher precedence. If value was still not found drawable directory is searched. It is in accordance with:
How Android Finds the Best-matching Resource
If drawable-en contains matching resource then drawable-hdpi and drawable would be eliminated based on step 4 of the algorithm.

I read the intro portion
assume the following drawable directories each contain different versions of the same images
In your example Android would ignore the drawable-en-port folder because it does not have that drawable name in it. It would only look in the folders that have that drawable, therefore selecting the best matching folder. It would only select 'drawable' if there were no folders with a matching configuration and image.

Related

How in delphi can i get a drawable resource ID?

in Delphi, I add image resources to the project (via project > resources and Images), however i need for one java function to give a resource ID in the application's package of the drawable to use. Is it possible to retrieve a resource ID (integer) from my delphi resource file or name ?
If not, how in delphi we can add a custom resource image and retrieve it's resource ID ?
Getting a Resource ID
To retrieve the resource ID of a resource:
id := TAndroidHelper.GetResourceID('my_image', 'drawable');
If you are using an older version of Delphi you may need to use the alternate, older approach:
id := TAndroidHelper.Context.getResources.getIdentifier(StringToJString('my_image'), StringToJString('drawable'), TAndroidHelper.Context.getPackageName);
The shorter, more recent (and convenient) GetResourceID function is merely a wrapper around the earlier longer winded version.
In either case, my_image is the filename (without extension) of the image resource whose ID you need.
The drawable parameter tells the helper that it is a drawable resource ID that you are asking for (as opposed to a string or layout, for example).
Adding Image Resources
To add a custom image resource to your project use the Deployment option of the Project menu in the IDE.
Add your file and set the remote path to a suitable drawable resource folder, e.g.:
res\drawable
res\drawable-xxhdpi
etc
You could put your own scaled versions of the image in each drawable folder for different resolutions. The remote_name must be the same in each case. The specific drawable folder determines the applicable device resolution.
Alternatively you could simply provide one suitably high resolution file. You can place this in either an appropriate high-resolution folder (e.g. drawable-xxxhdpi) or simply in drawable.
Either way, Android will auto-scale the images at runtime for other device resolutions as necessary.
There are lots of Android references on the subject of drawable scaling, alternative versions for different display types etc, including the Android documentation itself of course.
Referencing your comment, there are however no XML files required for adding drawables that are straightforward images.
Additional XML may be necessary for other types of drawable resources, but that will very much depend on the nature of your specific needs.
Bringing It All Together
In the screenshot below the highlighted entry is one that I have added for a PNG image resource. The file is in my project folder so there is no local path (the first column).
The filename is appstore.png and I have configured the deployment to place one copy of that file in the res\drawable' folder of the application when it is deployed (theremote` folder).
This file will rely on auto-scaling for display on different resolutions devices.
To get the resource ID of that resource I then simply write:
id := TAndroidHelper.GetResourceID('appstore', 'drawable');

What should be the directory of an Image in Android

I have an image that has to be shown by imageview what should be the perfect directory for loading it as bitmap from string .I have tried putting it resource folder but failed.It would be great if you provide folder hierarchy.
Resource images should be stored in a res/drawable variant according to these guidelines.
By variant I mean any density dependant (-hdpi, -xhdpi...), orientation dependant (-land, -port), or any other qualifier or combination defined in the Table 2 of the linked document (mind the order of the qualifiers, it does matter as explained in the document).
It is a best practice to consider /res/drawable as a fallback should no better qualified directory apply.
can you post your code? Typically I put my images in drawable folder. By displaying it in imageView you should get the id of the image you wanted to place.

How to access res/drawable/"folder"

On my application I have many pictures which I have grouped in folder under res/drawable. But Android dosen't let me access them trough "R". Is there a way to access those folders.
This is the Code im using for.
ImageView iv = new ImageView(conext);
iv.setImageResource(R.drawable.**smiley.666**);
I marked the part which I can't access.
Thx in Advance
safari
No, Android does not allow subfolders under /res/drawable: Can the Android drawable directory contain subdirectories?
You can however, add all image files under /drawable and then access them programmatically via:
int drawableID = context.getResources().getIdentifier("drawableName", "drawable", getPackageName());
iv.setImageResource(drawableID);
Where drawableName is a name of the drawable file, i.e. myimage if image file is myimage.jpg.
The code above will get image resource with id R.drawable.drawableName.
R.drawable.xxx is just a reference that the Android SDK generates in your R.java file. You are trying to make a sub-folder in your res-folder. The SDK can't generate a reference to any of your sub-folders, you have to put it in the predefined folders drawable-hdpi, -ldpi and -mdpi.
If you dont know how this works. I'll sum up. Android runs on a lot of different devices with a lot of different screen resolutions. With these three folders, you can have the same picture in three resolutions and depending on the device you are running your app on, one of these three folders will be used. You can read more here:
http://developer.android.com/guide/practices/screens_support.html
You can't create folders in res/drawable the system doesn't recognize those.
you need to save your image first and then make a xml for them so you can access it through R.your_pics
context.getResources().getDrawable(R.drawable.progressbar1);
Android - Open resource from #drawable String

Android button state files

The docs say to put XML state files for buttons in "the" "drawable" folder - which one of at least three?! (Putting it in res/drawable gives an out of sync filesystem error and putting it in each of the drawable-*dpi where * is l, m, h is an error too.)
res/drawable is ok and default.
The "fs out of sync" is probably from your IDE when you put the files e.g. via command line or into the folder. IDEs usually try to remember the state of files and report external changes this way. Try issuing a "refresh" command in the IDE.
res/drawable is a fallback that is taken if you do not provide more specific images in res/drawable-*dpi or also some orientation counterparts.
Have a look at the docs:
http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
drawable-nodpi is a special directory for files you don't want scaled, which makes no sense at all for buttons, as you want buttons to scale according to screen size/dpi.
You should use res/drawable for your XML state list drawables. If you get the "out of sync filesystem" error just refresh the Eclipse project (select it in the projects pane and hit F5).
XML state lists are (in most cases) not DPI-independent. However, their content will not change across different DPI environments. Basically, this means that if you reference a raw drawable called, for example, #drawable/btn_pressed, from within a state list, Android will look for the appropriate file for that drawable, according to the environment (drawable-*dpi/btn_pressed.png).
As you can see, although the state list is the same on LDPI, MDPI and HDPI, the drawables referenced within it could change.
I propose drawable-nodpi because it works.
However this is not made obvious in the docs when it needs to be.
How to make something trivial complicated? Make it ambiguous.

Complete list of all Android resource folders?

First I thought that I could find this list on the net, but I'm either looking for the wrong term or such list doesn't exist.
What I need is basically a cheat sheet of all predefined resource folders in an Android project. For example, a list could say something like this
res/drawable - all graphics go here
res/drawable-hdpi - all graphics of higher resolution go here
res/layout - some-meaningful-description
res/values - some-meaningful-description
res/layout-land - some-meaningful-description
etc.
I am really surprised that such list isn't easily found on the net. Whenever I need to add some resource I haven't used before, I have to look on the net for the correct naming (and I would rather look at the list of res folders).
Taken from here:
In the /res folder you can have:
animator/ -XML files that define property animations.
anim/ - XML files that define tween
animations
color/ - XML files that define a state list of colors.
drawable/ - Bitmap files / Nine-Patches (re-sizable bitmaps)
/ State lists / Shapes / Animation drawables / Other drawables
layout/ - XML files that define a user interface layout.
menu/ - XML files that define application menus, such as an Options
Menu, Context Menu, or Sub Menu.
raw/ - Arbitrary files to save in their raw form.
values/ - XML files that contain simple values, such as
strings, integers, and colors.
arrays.xml for resource arrays (typed arrays).
colors.xml for color values
dimens.xml for dimension values.
strings.xml for string values.
styles.xml for styles.
xml/ - Arbitrary XML files
Also see Accessing Alternative Resources for more specific device configurations (locale, dpi, size, aspect, orientation, etc)
Never thought about it, but you are right.
You can always create a new project based on available samples projects in eclipse and see they have chosen to organize it.
You can see how one of my project is organized.
my project in eclipse
It should be noted that these folder layouts are not written in stone. One can create a folder tree in whatever manner one chooses. This is just the default tree created with IDEs such as Eclipse and is, as a result, the most popular and often consistent format. If you find another format to fit you better then you are free to use it. Just reference properly in your code.

Categories

Resources