Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does View method getId() return?

Tags:

android

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?

like image 406
user1928115 Avatar asked Dec 26 '12 19:12

user1928115


People also ask

What does getId return?

getId() returns the android:id value, or the value you set via setId() .

What does getId mean?

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.

What does findViewById return?

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 .

Which method is get view ID in Android Studio?

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.


3 Answers

To get resource id name:

res.getResourceEntryName(view.getId())

You will get "field1"

like image 147
user2083234 Avatar answered Oct 15 '22 21:10

user2083234


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.

like image 39
CommonsWare Avatar answered Oct 15 '22 22:10

CommonsWare


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

like image 45
ArtemStorozhuk Avatar answered Oct 15 '22 22:10

ArtemStorozhuk