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
When you want to define an ID for a new View. You have to add the plus sign like: android:id=@+id/myView .
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With