Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Android View id supposed to be unique?

Tags:

android

Ok so something that I am confused with is whether Android ids need to be unique or not. Here is why the confusion arises:
Let's just say there is an Activity that has an TextView (android:id="text") and a Button (android:id="button"). The Button sets the text of the text view to a random text. So, to add a listener, I will retrieve the button as
Button b = (Button) findViewById(R.id.button) and then add listener to do the task.
Now, I can refer to this same TextView from a DialogFragment, a Fragment and what not with the same id R.id.text and all the changes will ever be applied to this text view without doubt.

In case of subclassing BaseAdapter, you need to override the getView where you do the inflating if necessary and if not, you make changes. Now, you retrieve the Views in almost the same way. Almost.
You do a convertView.findViewById(..).

If all the views need to have a unique id, how does changing the content of View in the getView not result in haphazard behavior ?
I mean, all the views inflated have the same id as defined in layout.

My understanding is that the ids are unique in the context of the view that inflated them. Since I am a beginner, I am asking for clarification

Update after Ahmad's answer

So, what this means is I can not inflate two Fragment in an Activity by using the same layout. This will result in an exception. Both are inflated by the same Activity, hence belong to the same instance. The ids will conflict

like image 280
An SO User Avatar asked Aug 05 '13 20:08

An SO User


People also ask

Is Android device ID unique?

The android Device ID is a unique code, string combinations of alphabets and numbers, given to every manufactured android device. This code is used to identify and track each android device present in the world.

Can two buttons have same ID Android?

This is no longer possible,android lint checks prevent you from adding same ids in the same xml file. A work-around would be to use the include tag to include other xmls with repeating ids.

What is unique in each Android device?

Secure#ANDROID_ID returns the Android ID as an unique for each user 64-bit hex string. It's known to be null sometimes, it's documented as "can change upon factory reset".

Can we change Android ID?

You'll have to fully format your device data to change your Android phone's device ID. As the device ID is generated when you first set up the device, resetting the phone will change the Android device ID automatically.


1 Answers

In documentation You may read

An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).

It means there will be no exceptions if You use same id for all Your views but obviously layout will get useless then.

FindViewById works simply by traversing a tree until it finds first element with searched id and returns it (or null if doesn't find). If You had few elements with same id in tree You will always get same element, the one that is first in tree.

You may have plenty of fragments inflated with same layout just like you have ListView with each element having same layout, that is because inflater doesn't care about id values. It simply reads XML file and create a tree with correct view objects nothing more.

like image 93
Gustek Avatar answered Oct 03 '22 21:10

Gustek