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.
Related
I am having a rather annoying scenario where I work with a lot of drawable shapes, many of which are slightly changed variants of others. The annoying thing is, when I paste the drawable, it automatically defaults to a random resource folder. See bellow photo. I want it to go directly to the standard drawable folder, not some other density drawable folders without having to manually change the folder every single time.
Is this even possible?
You can change the View from Android to Project if you are copying & pasting multiple images in a row. You can simply paste it to the drawable folder.
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');
I've had an app developed and have the responsibility of maintaining it, which means learning the Eclipse ADT environment. Nearly 20 years in web dev gives me some comfort, but this is certainly a new experience.
In one of the screens shown in the Graphical Layout window, a graphic source is indicated in the Properties panel as:
Src #drawable/ordo_search
ordo_search, obviously being the name of the PNG graphic, drawable appearing to be the folder.
But there are 4 folders holding graphics for this app, all beginning with the word drawable. They are:
drawable
drawable-hdpi
drawable-large-mdpi
drawable-sw600dp-hdpi
By altering this particular image and seeing the change come up in the Graphical Layout, I've determined that this graphic resides in the one called drawable-sw600dp-hdpi. In other areas of the app, I've determined in the same way that graphics are being pulled from any of the 4 folders, but in all cases the properties source paths all read the same: #drawable
Somewhere that #drawable attribute is being told an absolute path to where that graphic is, and that's what I need to find: where would I find and edit the path to that, or any, graphic?
Obviously I'm just getting to know the environment, so bear with me if you would.
It's not possible to get the path
This path will differ from device to device due to different dpi's of devices, it can point to any of the 4 folders you defined. If you want the drawable image you can get it via code by using getResources().getDrawable(R.drawable.yourdrawablename);
This will return your drawable and you can use it to display in a ImageView or where ever you want.
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.
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.