What is the co-relation between View's getId() method and the id defined in the XML file? Is there any? My IDs look like this: "field1", "field2"... This digits at the end are very important for my application and I need to retrieve them. Is there any way to do it?
getId() returns the android:id value, or the value you set via setId() .
The getId() method is used to return the thread identifier. The thread ID is a unique positive number which was generated at the time of thread creation. The thread ID remains unchanged during its lifetime. When the thread is terminated, the ID of thread can be reused.
findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .
The Android SDK provided a method: findViewById() . Functionality-wise, this method performs a singular task — it will give you the reference to the view in XML layouts by searching its ID.
To get resource id name:
res.getResourceEntryName(view.getId())
You will get "field1"
What is the co-relation between View's getId() method and the id defined in the XML file?
getId()
returns the android:id
value, or the value you set via setId()
.
My IDs look like this: "field1", "field2"
In your XML, they may look like @+id/field1
and @+id/field2
. Those are allocating ID resources, which are turned into numbers at compile time. These are the numbers you refer to in Java code as R.id.field1
and R.id.field2
.
This digits at the end are very important for my application and I need to retrieve them. Is there any way to do it?
Have a big switch statement comparing the ID to R.id.field1
, etc. Or, set up a HashMap
to map between the getId()
values and your "very important" numbers. Or, find some other solution that does not involve magic names on your ID values.
What is the co-relation between View's getId() method and the id defined in the XML file? Is there any?
From documentation:
Returns this view's identifier.
Related XML Attributes
android:id
Returns
a positive integer used to identify the view or NO_ID if the view has no 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