Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Android's R.id need to be unique?

Tags:

android

r.id

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?

like image 856
fadedbee Avatar asked Oct 25 '15 06:10

fadedbee


People also ask

Is there a unique Android device ID?

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.

What is the use of R ID in android?

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.).

What is the use of Android ID in Android Studio?

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.

What does ID mean in Android?

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.


1 Answers

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.

like image 191
sergej shafarenka Avatar answered Oct 06 '22 23:10

sergej shafarenka