Android Id Naming Convention: Lower Case with Underscore VS. Camel Case

Android id naming convention: lower case with underscore vs. camel case

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.

Best practices for ID naming conventions in Android?

Checkout --> https://github.com/umesh0492/android-guidelines

Further ID naming

IDs should be prefixed with the name of the element in lowercase underscore. For example:

+---------------------+
| Element | Prefix |
|-----------+---------+
| TextView | text_ |
| ImageView | image_ |
| Button | button_ |
| Menu | menu_ |
+-----------+---------+

view example:

<ImageView
android:id="@+id/image_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Menu example:

<menu>
<item
android:id="@+id/menu_done"
android:title="Done" />

The best guidelines I have ever seen and I do follow them.

IOS and Android package name

Following Java package naming conventions the reason for underscores for the Android package name is:

Naming Conventions

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

For iOS, many people use camelCase. The same case was used your iOS bundle ID or package name but there is no strict rule which case to use. This is only a convention and not a compiler rule.

Valid names consist of letters (lower or upper case), digits, underscores and start from a letter or underscore.

Should they need to be different?

Each platform follows its own conventions but there is no need for them to be different. As aforementioned - you can rename package as you wish using letters, digits and underscores.

What is the practice for package names for cross platform?

If possible - you can use the same package name but it will violate conventions, which is not desirable.

If not possible (e.g. project does not compile) - follow the rules.

IMHO, following conventions (as the also follow the rules) is the best solution.

You can find more information here about Java package naming conventions (this is a different resource).

Table Naming: Underscore vs Camelcase? namespaces? Singular vs Plural?

Being consistent is far more important than what particular scheme you use.

Are there conventions on how to name resources?

I don't know whether there are any official recommendations.

For ids in my layouts with widgets and containers, I use the convention:

<layout>_<widget/container>_<name>

I do the same strategy for any dimens, strings, numbers, and colors I use in those layouts. However, I do try generalizing. e.g if all buttons have a common textColor, I won't prefix the name with the layout. The resource name would be 'button_textColor'. If all textColors are using the same the resource it will be named 'textColor'. For Styles, this is usually the case as well.

For menu resources i use:

menu_<activity>_<name>

Animations are only different as you cannot use uppercase letters. Same goes for drawable xml resources, i believe.

JSON Naming Convention (snake_case, camelCase or PascalCase)

There is no SINGLE standard, but I have seen 3 styles you mention ("Pascal/Microsoft", "Java" (camelCase) and "C" (underscores, snake_case)) -- as well as at least one more, kebab-case like longer-name).

It mostly seems to depend on what background developers of the service in question had; those with c/c++ background (or languages that adopt similar naming, which includes many scripting languages, ruby etc) often choose underscore variant; and rest similarly (Java vs .NET). Jackson library that was mentioned, for example, assumes Java bean naming convention (camelCase)

UPDATE: my definition of "standard" is a SINGLE convention. So while one could claim "yes, there are many standards", to me there are multiple Naming Conventions, none of which is "The" standard overall. One of them could be considered the standard for specific platform, but given that JSON is used for interoperability between platforms that may or may not make much sense.

ID, id, or Id?

I do what I feel like. In general, your best practice is to err in favor of readability vs. compliance with some abstract standard.

Just be consistent.



Related Topics



Leave a reply



Submit