views:

1007

answers:

3

Hi,

I'm currently programming an application for the Android. Now what I found out is that you cannot place resource objects, say, an image in the drawable folder and name it like "myTestImage.jpg". This will give you a compiler error since camel case syntax is not allowed, so you'd have to rename it like "my_test_image.jpg".

But what about ids you define in the XML file. Say you have the following definition

<TextView android:id="@+id/myTextViewFirstname"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Firstname" />

This is a valid definition, compiles and works just fine on my Android emulator although - as you see - I'm specifying the id in camel case syntax.

Now, the Android samples always use lower case and underscore. Is this just a naming convention to use lower case with underscore for the id's or may it cause problems on the real device?

Thx

A: 

If the Android's compiler is truly doing what you say restricting camel case (which seems rather odd) then you should stick to the established conventions.

Going against the grain will only cause unnecessary confusion. Keep things consistent in all places where possible.

Brock Woolf
You probably misunderstood what I was trying to say. The compiler will complain if you put resources like a file and write that in camel case. The other case however is when you specify IDs in the XML layout files. There you have the possibility to place camel-case id names and the emulator works just fine.Now as I mentioned, the Google samples are all in the form my_id_name, but there are a lot of other samples around having camel-case id names...
Juri
+2  A: 

The device will not complain if you use camel-case id names. For my first application I wrote all the ids in camel-case because I think it appears better in the Java code that way, and it works just fine.

I am slowly changing my mind on camel-case, though, because you end up with two different naming conventions - for example:

// This must be undescored due to naming constrictions
setContentView(R.layout.my_long_layout_name);

// Now this looks a little out of place
findViewById(R.id.myLongSpecificId);

I, too, wonder about the standards here. Google is inconsistent in their examples; sometimes they use all lowercase, sometimes they insert underscores, and sometimes they use camel-case.

Daniel Lew
Yes, that's exactly my issue. They force you to use the underscored naming convention for layouts while you can use either camel-case or underscored for the ids referencing controls/widgets inside the XML layout definitions.Google should really define some standard here (if they didn't already, at least I didn't found anything).So going for one way is sure the best to be consistent throughout the application whether you reference layouts or id-referenced fields.
Juri
Just for curiosity: Do you have a link where Google is inconsistent, meaning where they used the camel-case notation for ids?
Juri
Just a bit of random perusing and I came across this... http://developer.android.com/guide/samples/ApiDemos/res/layout/list_12.html
Daniel Lew
A: 

xml file names (which is what is used in the drawable folder) must be all lower case separated by the underscore character _ since capitalized file names are not supported in xml.

Android Coder