This has been a topic that was bugging me for a while because there seems to be no convention while managing the naming that you assign to various UI elements in Android. Any attempt to define a convention has to take into account the following :
- How do you handle name clashes since all of the names go into R.id ?
- Add element type at end? So, for example, is it
loginEditTxt? Do you use this only when there’s a potential collision? Or with every UI element?
- How do you handle autocomplete? Eclipse can autocomplete names, but it’s not much good if you have a project with ~ 50/60 UI elements defined in the layout files?
- camelCase or underscore seperated to name layout files? Since Android doesn’t support capitalization in layout file names, which one would you use?
My responses are below:
Namespace clashes / Autocomplete: Use screnname_varname. So you’re on a screen that lets you login, you’d name your textfield:
ElementType ending: I’d like to use it since I enjoy Apple’s philosophy of clarity over length. So it’s always
login_loginTextView. This doesn’t have to say
TextView completely, it just has to indicate the intent of the element so it makes it easier when you get back to the code a month later to look at the screen and you can’t figure out which element is what until you open the layout file and look at it.
Autocomplete: The system above handles autocomplete well since typing out just your screenname will restrict the autocomplete output to relevant UI elements.
camelCase or under_score: Underscore to separate screnname and elementname. Element name in camelCase because that’s the java convention. The only reason I’m doing this is because
loginLoginTextView sounds weird.