Android naming identity: underscored with underscore against camel

I am currently programming an Android application. Now I found out that you cannot place resource objects, say, an image in a drop-down folder and call it "myTestImage.jpg". This will give you a compiler error since the camel case syntax is not allowed, so you will need to rename it as "my_test_image.jpg".

But what about the identifiers that 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 the correct definition, it compiles and works fine on my Android emulator, although, as you can see, I specify the identifier in the case camel syntax.

Android patterns now always use lowercase letters and underscores. Is it just a naming convention for using lower case with underscore for an identifier, or can cause problems on a real device?

thank

+80
android mobile
Dec 02 '09 at 11:17
source share
9 answers

The device will not complain if you use camel identifier names. For my first application, I wrote all the identifiers in a camel case because I think it looks better in Java code this way and it works great.

I slowly changed my mind about the camel case because you have 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 also wonder about the standards here. Google is incompatible in its examples; sometimes they use all lowercase letters, sometimes they insert underscores, and sometimes they use a camel case.

+81
Dec 02 '09 at 15:30
source share

If you look at the android.R.id.* Fields, you will notice that they are all in a camel case. Therefore, if the android ids are in the camel case, I think we should follow this convention :)

+12
Feb 19 '14 at 11:00 a.m.
source share

I think it talks about identifiers inside an xml file.

eg:

 android:id="@+id/home_button" 

against

 android:id="@+id/HomeButton" 

I did not find any agreement or recommendation in this matter, so the developers in my project use both methods indistinctly, which is rather painful :(

+9
Aug 24 '11 at 15:02
source share

I think it's good if we use all lowercase letters with underscores.

Just look at this (addition to what Daniel 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 confuse the camel case convention in xml a bit, because when you link it to Java, which also uses the camel case (because it's standard), it looks like a doppleganger.

+4
Jun 05 '13 at 6:32
source share

If you look at some examples of Google applications, for example:

https://github.com/google/iosched

They use underscores. So ... maybe we should do this?

+4
Apr 30 '16 at 16:01
source share

The xml file names (which are used in the portable folder) must be all lowercase, separated by the underscore _, since file names with uppercase names are not supported in xml.

+1
Oct 28 2018-10-28
source share

If the Android compiler really does what you say, limiting the case of a camel (which seems rather strange), you must adhere to established conventions.

Confronting the grain will only cause unnecessary confusion. Keep things consistent wherever possible.

0
Dec 02 '09 at 11:21
source share

I think that if we use the underscore convention for id in xml files and the camel convention for class fields, this will give better visibility to each developer to distinguish between xml identifiers and class fields.

0
Feb 23 '16 at 11:20
source share
 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 definite standard that would determine which one is more valid, but I have several reasons to prove it. My idea is to store the XML identifiers and the Java variable in the same name with the camel legend.

  1. Retrieving a variable is easy by searching for a project in both XML and the Java side.

  2. butterKnife library definition

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

It is more correct to continue in this way.

0
Mar 28 '18 at 14:00
source share



All Articles