Do Android's R.id need to be unique (as I've kept them so far)?
Or is it OK to reuse an R.id across different views?
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.
R. id. text1 is an used for TextView views. You can find it in many layouts from the framework ( select_dialog_item , select_dialog_singlechoice , simple_dropdown_item_1line , etc.).
android:id. Resource ID. A unique resource name for the element, which you can use to obtain a reference to the ViewGroup from your application.
A device ID (device identification) is a distinctive number associated with a smartphone or similar handheld device. Device IDs are among one of the easiest ways to identify mobile users.
ID's are used for finding views inside a view hierarchy. Android uses depth-first search algorithm - meaning it looks to the very bottom of one tree branch, then to another one etc. If you have two views with same ID, then the algorithm will find first view and stop searching further.
There is no strict requirement on uniqueness of ID. For instance, when you have a list view, then each item of that list will be inflated using same layout and in most cases will have same shared ID, which is totally ok.
Keeping that in mind, if you have two (or more) views sharing same ID, you should help Android to pick up the right one. For that you will first need to search for the correct parent of that view, and then for the view itself.
For instance, if you have two views with the same ID in two different fragments, then you should first search for fragment container view, and then for the view with shared ID inside that container.
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