I have set some conventions to avoid assignment of duplicate ids to different views. For example I use a prefix that is sort of abbreviation of the layout to the child views and use suffixes like Btn (stands for Button
) and IV (stands for ImageView
. It almost guarantees the uniqueness of the ids to a high level, but is there any (almost) formal (or common) conventions like camel naming in this case?

- 7,177
- 5
- 42
- 66
-
I use a 3 characters naming convention derived from my VB NET times: btn, img, lvw, txt, edt, ... – Phantômaxx Nov 16 '15 at 17:25
-
@FrankN.Stein Indeed I prefer 3 character abbreviations as well, and it works quite well. But, what do you do if you have to define say two `cancelBtn` in different layouts? How do you maintain uniqueness without being forced to keep everything in mind? – Mohsen Kamrani Nov 16 '15 at 17:28
-
In different layouts? I could prefix them with the layout name. i.e.: `dlgAlert_btnCancel` and `dlgConfirm_btnCancel`. It has quite a logic, so it doesn't force you to mental acrobacies. ;) – Phantômaxx Nov 16 '15 at 17:35
-
1@FrankN.Stein Yes, in different layouts.So I'm not alone :) I'm doing it too, and it seems to be a common approach. – Mohsen Kamrani Nov 16 '15 at 17:39
-
Then... problem soved, I guess. – Phantômaxx Nov 16 '15 at 17:41
-
@FrankN.Stein I donno. Maybe someone has a better solution. Btw, how do you know that you have already defined btnCancel and then add the layout name? (Do you keep it in mind? :) ). You should use it for every view always or remember every thing. Think about the case someone else creates some layouts. – Mohsen Kamrani Nov 16 '15 at 17:44
-
Well, it turns out that it's not really a problem: http://stackoverflow.com/a/12333707/2649012 – Phantômaxx Nov 16 '15 at 17:46
-
@FrankN.Stein What if you import two layouts? – Mohsen Kamrani Nov 16 '15 at 17:50
-
You mean such as two fragments? Well, it's (probably) like when you fill a custom ListView - All the items will have the same id(s), say txtListItem... – Phantômaxx Nov 16 '15 at 18:18
1 Answers
Personally, I prefix my IDs with the entire View name, rather than an abbreviation
ex. button_login
I feel like this is more readable, as long as the names don't get too long.
Seeing as Android resources can't contain capital letters, snake casing is probably preferred for IDs as well. (Although I've seen other people, including myself, use camelcasing out of preference)
The example in the docs is kind of terrible for naming conventions, considering they call their button my_button
, but they do use snake casing, and the name of the view. (Although I agree with you that it should come first)
In general, I think it really depends on personal preference, and who else might be reading your code. If you think that abbreviations such as "btn" are good, I'd say go ahead and use them!
As long as you establish one convention, and stick to it, you're fine.

- 12,073
- 8
- 39
- 55
-
Seems you are too diligent :) But, as I asked @Frank N. Stein what do you do if you have to define two say `cancelBtn` in different layouts? How do you maintain uniqueness without being forced to keep everything in mind? – Mohsen Kamrani Nov 16 '15 at 17:31
-
Take a look at this [answer](http://stackoverflow.com/a/6495504/2278598) I'd say the best practice is to try to make all of your IDs unique, but if you absolutely can't its okay. I prefer not to prefix them with the layout name because it makes the names super long, but if you're comfortable with that, go ahead. – Andrew Brooke Nov 16 '15 at 17:39
-