Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Possible to get a custom R.id

Tags:

android

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 
like image 732
tjb Avatar asked Aug 15 '11 15:08

tjb


People also ask

How can I create ID in android?

When you want to define an ID for a new View. You have to add the plus sign like: android:id=@+id/myView .

What is Android R id content?

android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.

What is R Id home?

But android. R. id. home is an ID in Android's R file - you can think of it like a separate set of system resources.

What is the R class in android?

R is a class that contains ONLY public constants. (public static final). It is a generated class (by Android Plugin in Eclipse) that reflects the various values you defined in the res file. Read the Resource guide in Android Developers for more information about this.


1 Answers

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).

like image 123
inazaruk Avatar answered Oct 11 '22 14:10

inazaruk