tjb
tjb

Reputation: 11728

Android: Possible to get a custom R.id

Is it possible to get android to give me a custom id?

so for example if I already have defined in xml:

R.id.some_layout
R.drawable.some_drawable

is there any function like this

R.custom_id("a_custom_id")

so I could then access as

R.id.a_custom_id 

Upvotes: 6

Views: 4258

Answers (2)

inazaruk
inazaruk

Reputation: 74780

You can not dynamically create new IDs. Even if R was capable of doing so, you wouldn't be able to access it using R.id.a_custom_id. Java is not dynamic language, and cannot add fields at runtime.


There is, however, compile-time solution. In your res/values/ids.xml add:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <item type="id" name="a_custom_id"/>
</resources>

And then you can reference R.id.a_custom_id in your code and "@id/a_custom_id" in xmls. Of course its still pre-defined id (as opposed to runtime-defined id).

Upvotes: 25

Ivan Gromov
Ivan Gromov

Reputation: 4415

You can create boolean, integer, dimension, color, and other array resources. http://developer.android.com/guide/topics/resources/more-resources.html

Upvotes: 1

Related Questions