Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Resource IDs

I'm retrieving custom Resource IDs from a custom xml view type. I'm asked to specify a default int value for the retrieval and was wondering what is the range of IDs? Are they always positive or do they include zero??

i.e is -1 a valid "null" reference AND/OR is 0 a valid "null" reference?

Thanks

EDIT

Custom XML resource/attribute file

<resources>
    <declare-styleable name="ToggleImageButton">
        <attr name="onImage" format="integer" />
        <attr name="offImage" format="integer" />
    </declare-styleable>
</resources>

Defined in my constructor for my custom ui

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleImageButton);

int offResource = a.getInt(R.styleable.ToggleImageButton_offImage, -1);

Basically the -1 at the end of the 2nd line is the default parameter for this data type. It may or may not be initialized in the XML view when developing and this allows default behavior to be specified this way.

like image 879
Kurru Avatar asked Feb 27 '11 01:02

Kurru


People also ask

What is a resource ID in Android?

For each type of resource, there is an R subclass (for example, R. drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.

What are the resources in Android?

In Android, a resource is a localized text string, bitmap, layout, or other small piece of noncode information that your app needs. At build time, all resources get compiled into your application. The resources locates inside res directory of your app.


3 Answers

According to the documentation, Resources.getIdentifier()

Returns 0 if no such resource was found. (0 is not a valid resource ID.)

UPDATE (after 5+ years thanks to Micer):

  • API 29+: Resources.ID_NULL constant
  • older API: ResourcesCompat.ID_NULL
like image 107
Ewoks Avatar answered Oct 22 '22 06:10

Ewoks


0 is a null/invalid value for a resource ID.

like image 33
adamp Avatar answered Oct 22 '22 05:10

adamp


According to https://developer.android.com/reference/android/content/res/Resources#ID_NULL, 0 is the same as setting @null in XML, meaning you can use it i.e. when you want to clear the resource.

It is an invalid resource ID.

like image 22
Micer Avatar answered Oct 22 '22 06:10

Micer