Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's your naming convention for the View IDs?

Tags:

android

I believe that many of us were bothered while naming the View IDs. Unlike the package mechanism, the IDs of the resources in a project are using one common namespace. Therefore, we have to figure out some ways to name the fields with the same functionality but in different layout files.

My way is to add the noun or the verb that the layout file's Acitvity class name used in the front of the original ID, separated by a dot. For example,an ID originally named "description" in a Activity displaying a movie's information may become "movie.details.description".

Is there any better ideas?

like image 507
shihpeng Avatar asked May 27 '11 09:05

shihpeng


2 Answers

First I used the same concept but now I try to use more generic ids. In you case I would simply call the id description. So you don't need an id for each element and can reuse them as it doesn't matter which layout you're referencing when you call findViewById(R.id.description) you know you get the description element of this layout.

Of course you have to know if you layout has a description element at all. This might be a disadvantage.

like image 181
Flo Avatar answered Oct 12 '22 19:10

Flo


Interesting question. Given there is no official style guide that has been released by Google (or the community, which I may be wrong about it but I just didn't find one on Google), then it could be slightly subjective. You have to use the style that makes you and your team comfortable.

My naming convention is the following:

activityname_elementtype_explicitdescription_suffix

For instance a button Login defined inside the activity Home may become:

home_btn_login

like image 42
Amokrane Chentir Avatar answered Oct 12 '22 17:10

Amokrane Chentir