Android button state files - android

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.

Related

Android Studio: Is is possible to set default drawable folder?

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.

How to get images(same name) from different drawable folder in a android project?

I need to get same images with same name, but in different resolution. So i created different drawable folder for every resolution and having images with same name in all folder. But when i am running this aap at that time some images coming dynamically at diff resolution. I have to bound images, should comes from related resolution folder at which device it is running at that moment. I used some images as theme, these are making trouble for me. :( :(
I created drawable folder in this way.
drawable-sw600dp-land-mdpi.
drawable-sw600dp-port-mdpi.
Please help me, I am indeed.
If your folders are named correctly, android will take care of that by hisself. If the device is rotated, the activity is destroyed and recreated again. Depending on on current state (landscape or portrait), android loads the drawable from the corresponding folder and you only have to say "load a drawable" :)
If the corresponding folder doesnt exist, android falls back to a default one.
Try to change
"drawable-sw600dp-land-mdpi" to "drawable-sw600dp-land"
"drawable-sw600dp-port-mdpi" to "drawable-sw600dp-port".
Remove "mdpi" from directory names.
create drawable folder like this
drwable-land-mdpi
drawable-port-mdpi
drwable-land-hdpi
drawable-port-hdpi
and also see this link google resources

Puzzled Over Android App Graphic Source Path

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.

Resources NotFoundException?

I'm getting crash reports from android marketplace:
android.content.res.Resources$NotFoundException: Resource ID #0x....
I get about 17 of these a week. It's pointing me to the following in my code:
context.getResources().getDrawable(R.drawable.foo);
That resource is definitely present in my /drawable folder. I have several hundred thousand installs, I'm not sure how this could be failing for some users, but working for the vast majority. I'd like to find out what's going on, because they can't use the app in this state. Any ideas?
Thanks
--------- Update ----------------------
Also I can see the entry for the drawable in question in my R.java file, looks like:
public static final int foo=0x7f020034;
I do a clean build of the project, then straight after that do my release build (no code modification in between to give the automatic eclipse build stuff possibly let the R file go awry)
Thanks
I got this exception:
Resources$NotFoundException: String resource ID
when I was using setText with an int value. I had to convert it to String.
Before:
myTextView.setText(obj.SomeIntProperty);
After:
myTextView.setText(String.valueOf(obj.SomeIntProperty));
Is the crash reports coming from version <=1.6, and you have certain resources only in qualified folders, i.e. "drawable-mdpi" instead of just "drawable"? If so then read about "Known issues" at the bottom of this page.
The same problem I found on my application today morning and found a very simple solution to this. the problem looks big and complicated but it's not.
Steps which I followed are
Delete the bin folder it self
Clean the project
Remove application from your device
Run the application and check your scenario.
According to me it should not give this exception again.
Reason : When you build the application R.java contains all the memory address and it happens that those address are invalid for next run.
Second Reason : When you have the application which supports lanscape and portrati mode both and if you have not wrote the XML file for either of them then it's the possibility that may lead to this kind of crash. and there are pretty much chances of OutoFmemory and Memory leak issues when you have to support both the modes. please think on this as well.
This have occurred several times especially on phones with low density. I also noticed that it mostly happens with 9-patch images.
My solution was to include 9-patch images in as much density resource folders as I could (mdpi, ldpi, hdpi, xdpi).
You have included certain codes that reference content from Android SDK.
For example, I faced a similar issue once and when I removed the line
android:background="?android:attr/actionBarItemBackground"
from my xml, everything works fine again.
Heh, no idea, I'm actually working on a similar issue, but it could be it. It's worthwhile mentioning that while the device can upscale resources and layouts, it cannot downscale them. So if you have minimum values, set them in the AndroidManifest.xml. Try adding layout-ldpi and layout-small first.
A useful technique for logging which layout got loaded is to attach an android:tag to every root container of your layout XML file, and in onCreate() after setting setContentView(R.id.layout) just print the value of the tag. It will tell you which got loaded - you still have to plug the hole by adding all possible combinations, if just for debug purposes.
also, rename your drawables to drawables-nodpi folder to ensure no drawables are missing. this turns off internal scaling, makes APKs smaller and doesn't "pick" from any other drawable folder.
once you figure out whether it's the layout or drawable, additional metrics will help you find the root cause. for now, check the layouts first with "always use" drawables.
on monday when i get back to the office I'll post you the metrics code used to measure data captured in this thread - Which part of Android is in charge of picking a correct resource profile?
In my case I had a layout file that lead to the crash:
I got the following log-
That showed this link to my actual code:
Which is this line of code:
I checked my file navigator folder names as suggested by the most popular answer and it looked like the resource that is supposedly missing is in the normal layout folder
but when looking under the Project file navigator rather than the Android file navigator I was able to see that not only was the supposedly missing resource file not in the normal layout folder, but I didn't even have a layout folder without a special extension.
Adding an Android Resource Directory to the res folder and calling it layout and then pasting over the resources from your speciality folder (ex. layout-sw720dp) into it fixed the issue for me.
This solution will also work for other folders as suggested such as not having files in drawable and only having them in drawable-xxhdpi can be fixed by moving the file into drawable and tweaking it as needed. Good luck folder fiends!
This could also happen if the resource you are referring to (lets call it ResA) is in-turn referring to a resource which is missing (lets call it ResB).
Android will raise the ResourceNotFoundException for ResA even though whats really missing is ResB. Such is life!
In my case, ResB was defined in the values-swxxxdp but not in values. Hence I was getting this exception on phones but not on tablets.
The error can happen when portrait XML layout file is not defined. It doesn't happen all the time, but happens for example if user quickly turns off and on the phone, I suppose in that case the system calls the onCreate in portrait orientation, before calling onCreate in landscape.

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