100

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

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Juri
  • 32,424
  • 20
  • 102
  • 136

9 Answers9

91

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.

Dan Lew
  • 85,990
  • 32
  • 182
  • 176
  • 22
    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 Dec 02 '09 at 20:30
  • Just for curiosity: Do you have a link where Google is inconsistent, meaning where they used the camel-case notation for ids? – Juri Dec 03 '09 at 10:00
  • 8
    To confuse things further, the style names (which are like IDs for styles) in the project templates use PascalCase, e.g. `AppBaseTheme` and `AppTheme`. – Edward Brey Oct 29 '13 at 15:48
  • 6
    The underscore method is generally considered much less readable in the teams I have been involved in. The somewhat strange limitation of the resource files names not being allowed to be in camel case should not pollute your thinking for the rest of the Java interfaces - camelCase is firmly ingrained and I dont think anyone would thank you for forcing in a new "_" style in naming conventions. – RichieHH Mar 14 '14 at 14:53
  • 3
    @RichardRiley I find that interesting, as with underscores the word boundaries are properly defined while with camel case they're squished together so I have an easier time parsing underscore-separated names than camel-cased ones. – JAB Apr 10 '14 at 17:00
  • Here's an example of inconsistency https://github.com/googlesamples/android-architecture/blob/todo-mvvm-live-kotlin/todoapp/app/src/main/res/layout/taskdetail_act.xml – Vibhor Mahajan Aug 28 '18 at 13:23
14

If you take look at android.R.id.* fields, you will notice that all of them are in camel-case. So if the android ids are written in camel-case, I guess we have to follow this convention :)

Kiril Aleksandrov
  • 2,601
  • 20
  • 27
  • 1
    They are in camel case there because they were created in camelCase. This does not set any coding style precedent for the community at large and this is not linked to the naming conventions of resource files vs those of ID strings which is what the original poster is asking about. – RichieHH Mar 14 '14 at 14:50
  • 3
    Yes, they were created in camel case. But these IDs are from the android API itself so if the creator of the API have used camel case I guess it is a good approach to follow his convention. – Kiril Aleksandrov Mar 17 '14 at 09:10
  • 9
    there `widget_frame` field [http://developer.android.com/reference/android/R.id.html#widget_frame] also in `android.R.id.*` fields. in this field Google use underscore not camel-case So your conclusion about camel-case convention is right choose for ID convention may be false – ahmed hamdy May 20 '15 at 15:39
8

i think it is good if we use the all small letters with underscores.

Just look at this(Adding to what Daniel had answered)

  // Camel Case
    TextView tvUserName = (TextView) findViewById(R.id.tvUserName);
    // Small Caps and Underscores
    TextView tvUserName = (TextView) findViewById(R.id.tv_user_name);

in my own experience I tend to get a little confused of the camel case convention in xml because when you link it to Java which also uses camel case(because it is the standard) it looks like a doppleganger.

Rogene Sagmit
  • 101
  • 1
  • 8
  • 2
    This is very subjective and doesnt answer the Q as to why you cant similarly namee resource files as you can ID strings. – RichieHH Mar 14 '14 at 14:48
5

If you look at some of Googles app samples such as:

https://github.com/google/iosched

They use underscores. So.... maybe that is how we should be doing it?

Micro
  • 10,303
  • 14
  • 82
  • 120
  • But if you see other layout files then you will find that some of the ids of the views are with camel case. So I guess it totally depends on the developer side what would be better for the team. here is the link in which there is a camel case used for Recycler ID. https://github.com/google/iosched/blob/main/mobile/src/main/res/layout/fragment_agenda.xml – Dharmesh Baldha Mar 09 '22 at 12:44
  • There are all sorts of engineers at Google writing these sample apps. Some may use camel and it may slip by the engineering reviews (since it isn't a huge deal). But if you look at the majority of android projects across their github, you will see underscores. – Micro Mar 10 '22 at 09:13
4
android:id="@+id/frag_account_button"
frag_account_button = ((ListView)view.findViewById(R.id.frag_account_button));

android:id="@+id/fragAccountButton"
fragAccountButton = ((ListView)view.findViewById(R.id.fragAccountButton));

First of all, there is no certain standard to define which one is more Proper but I have my own idea about that. My idea is reasonable to keeping XML id and java variable in the exact same name with the camel-case convention.

  1. It is easy to reach the variable by searching for the project both XML and java side.

  2. butterKnife library definition

    @BindView(R.id.infoTextView) TextViewFont infoTextView;

It is more proper to keep in this way.

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
  • 3
    [Kotlin's view binding](https://kotlinlang.org/docs/tutorials/android-plugin.html#view-binding) returns the same as butterKnife, so I'm dropping snake_case altogether. – Rik Martins Jul 03 '19 at 09:03
2

I think if we use underscore convention for id in xml files and camel case convention for class fields then it will give better visibility to every developer to distinguish between xml ids and class fields.

Yasir Ali
  • 1,785
  • 1
  • 16
  • 21
1

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.

Paul
  • 120
  • 1
  • 8
  • 2
    This is not about file names, and besides, some people prefer not to capitalize the first letter when camel casing. – Jesper May 07 '12 at 07:55
  • It's a strange inconcistent issue IMO. You cant have case sensitivity to distinguish, no, but why not just ban resource files having the same name root component in the same directory. – RichieHH Mar 14 '14 at 14:44
  • (Btw it IS about file names vs resource IDs : recheck the OP) – RichieHH Mar 14 '14 at 14:54
0

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
  • 46,656
  • 50
  • 121
  • 144
  • 1
    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 Dec 02 '09 at 11:31
  • View Bindings actually convert IDs to camelCase so I am thinking that is the way to go. However I find snake_case a lot easier to read. – Ben Edwards Sep 20 '22 at 07:30
0

If you see within Android Sample of ViewBinding layout files then you will find that ids of the views are with camel case.

So I guess it totally depends on the developer side what would be better for the team.

Dharmesh Baldha
  • 820
  • 6
  • 11