On Aug 10, 2011, at 7:23 PM, chobo2 wrote:
> I need to find the id by name then use that int value to set a image.

Why do you need to do this?

> I found this method that seems to be what I need(it says I should not use
> this method if possible but not sure what other method I could use then).

The recommended approach would be to directly use the integer constants, which 
you appear to be loathe to do. The reason is probably performance, as using an 
integral constant ~directly is likely to be far faster than loading up whatever 
metadata mechanism Resources.GetIdentifier() is using.

> int result =  Resources.GetIdentifier("my_image, "drawable",null);
> 
> I was not sure what to put for package so that might be why result is always 
> coming back as zero.

The package is your application package name. :-)

For example, in the monodroid-samples/HelloWorld [0] sample, the package is 
"mono.samples.helloworld". Thus, within HellAndroid.OnCreate()[1], we can do:

        Android.Util.Log.Debug ("*jonp*", "Resource.Layout.Main={0}", 
Resource.Layout.main);
        Android.Util.Log.Debug ("*jonp*", "Resource.Layout.Main={0}", 
Resources.GetIdentifier ("main", "layout", "mono.samples.helloworld"));

These both print out the same value.

You can determine the package name by reading the verbose build output, or by 
reading obj/Debug/android/src/R.java:

        package mono.samples.helloworld;

(Generally speaking, the package name is your lowercased assembly name IFF your 
assembly name contains '.'s, e.g. `Mono.Samples.HelloWorld.dll` generates a 
package name of `mono.samples.helloworld`. If your assembly contains no '.'s, 
then your package name is `assembly_basename.assembly_basename`.)

HOWEVER, Android.Content.Res.Resources.GetIdentifier() wants the "Android" 
resource name, which may differ from what you expect. In your earlier email, 
you had wanted to do:

        TextView lblToday = FindViewById<TextView>("lblToday");

Android doesn't support mixed casing on several resource types, in particular 
resource types which are backed by distinct files (layouts and drawables, among 
others). Consequently, while Mono for Android would allow you to reference 
`Resource.Layout.lblToday`, Android will not, and you will need to lowercase 
such references to "lbltoday". (This doesn't extend to everything, though; ids, 
strings, colors, etc. may all be mixed case. No, I can't explain why Android 
did it this way.)

Consequently, as long as you always name your resources lowercase, things will 
be consistent, but if you use PascalCased filenames, then your 
Resources.GetIdentifer() call will need to use the lowercased variant of your 
resource names.

> I have this image in "Drawable-mdpi" but I would think "drawable" would work.

Correct, "drawable", would work.

 - Jon

[0] https://github.com/mono/monodroid-samples/tree/master/HelloWorld
[1] 
https://github.com/mono/monodroid-samples/blob/master/HelloWorld/HelloWorld.cs#L11

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to