Are there conventions on how to name resources?

Solution 1:

Android SDK will be a good place to start.

For example, I try to scope IDs within the activity.

If I had a ListView it simply would be @android:id/list in all the activities.
If, however, I had two lists then I would use the more specific @id/list_apple and @id/list_orange

So generic (ids, ...) gets reused in the R.java file while the unique ones (sometimes gets reused) get prefixed with generic ones separated by an underscore.


The underscore is one thing, I observed, for example:

Layout width is layout_width in xml and layoutWidth in code, so I try to stick to it as list_apple

So a Login button will be login, but if we have two logins then login_foo and login_bar.

Solution 2:

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.

Solution 3:

Taken from Android's documentation. There is more there on the subject.

Solution 4:

To answer your question: Yes, there are.

You can find many of them via google search for example. And there is no such thing as the best naming convention. It always depends on your needs and your project attributes (most importantly the scope).


Recently, I've read quite good blog post about naming resources in Android XML from Jeroen Mols. Author mentions the basic principle all resources should follow and then how this convention is applied to each resource type. Both described on Android resource naming cheat sheet:

Android resource naming cheat sheet

He then describes each element and each resource type in detail.


I would say you could use this convention from small to medium projects (personal use, few months contract applications). Although, I would not recommend it for long time projects with like 50+ activities or 1000+ strings.

Conventions for resource values in projects of such a large scale requires more investigation on how they will be used. Take strings for example. Might be affected by size of your team, translation center you are using (if any), VCS you are using (to avoid merge conflicts for example), etc. You might even think about splitting strings into multiple files.

I assume you are looking for something to begin with. So I would recommend the blog post I mentioned. It's good for starters and you can definitely use it as inspiration to create good naming conventions of your own.

Also keep in mind that as a project grows, many needs and requirements may change in time. So its completely normal that naming conventions that were suitable in the beginning will not be suitable after 2 years. And it's completely fine. You should not try to predict future. Just choose a convention and follow it. You will find if it is suitable for you and your project. If its not, think about why it is not suitable and start using something else.