Namespace management in Android layout XML Files

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 loginTxtView and 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: login_loginTextView or login_pwdTextView

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.

Advertisements

One thought on “Namespace management in Android layout XML Files

  1. Found this by googling android namespace clash. I’m surprised I’m not finding more information on the topic. I’ll give your suggestions a try – thanks 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s