Ollie C
Ollie C

Reputation: 28509

What is the clearest way to name resources in Android?

What naming conventions do you use for resource files and IDs and what benefits do they bring?

It's very easy for example to end up with a view ID like "activity_settings_location_text_label" but quickly becomes unmanageable and messy. I've not seen any guidance from Google on this, did I miss it?

Resources can be "grouped" using hierarchical naming, but what structure works best for each resource type? String, color, dimens, layouts, includes, etc

My naming is currently fairly ad-hoc, and it's clear I need better structure (particularly so I can find IDs more easily using the IDE's code-completion).

Upvotes: 15

Views: 5871

Answers (5)

Krimson
Krimson

Reputation: 7664

So 4 years later... Here is a list of 'best-practices' I adhere to when developing for android.

Here is the section regarding resources


For example when naming **`string`** resources, these are the goods and bads

#strings.xml

Name your strings with keys that resemble namespaces, and don't be afraid of repeating a value for two or more keys. Languages are complex, so namespaces are necessary to bring context and break ambiguity.

##Bad

<string name="network_error">Network error</string>
<string name="call_failed">Call failed</string>
<string name="map_failed">Map loading failed</string>

##Good

<string name="error_message_network">Network error</string>
<string name="error_message_call">Call failed</string>
<string name="error_message_map">Map loading failed</string>

**P.S** If you are using Android Studio, refactoring is a total breaze. It works for almost everything. Not just java code.

Just place your cursor's caret on an ID and hit SHIFT+F6 to rename and automatically refactor. Useful for changing IDs on the fly

Upvotes: 3

hpique
hpique

Reputation: 120324

Since there are no official guidelines about this, I'd recommend.

  • Be consistent.
  • Be practical.
  • Whenever possible, try to follow the style of the Android source code and android.R.

Upvotes: 3

C0deAttack
C0deAttack

Reputation: 24667

My approach, which has been working quite well is where a resource should be put in a particular XML file, for example you would typically put a Color definition in colors.xml or a string of text in strings.xml the resource file is part of the path, so instead of:

R.string.home_button_text

I would have:

R.string.home_button

I know it's a literal string because of the R.string part. To me this is more readable than putting "text" on the end of the id.

On occasion however it might not be obvious what resource type something is, in which case I make the id a bit more descriptive.

Upvotes: 2

Aman Aalam
Aman Aalam

Reputation: 11251

I do it like: home_search_btn that means its a a button, meant for searching something, and will be used in the activity named Home

Upvotes: 2

Dave
Dave

Reputation: 6104

The most important thing to remember, I think, is that it's perfectly fine to use the same ID across multiple layouts. For example, @id/title_bar is clean and generic and works, and so much simpler than @id/settings_title_bar, @id/home_screen_title_bar, @id/search_title_bar and so on.

I also like to name layouts destinated for activities as @layout/activity_home and @layout/activity_search etc. Drawables and icons should adopt the same standards as Android uses, i.e. @drawable/ic_btn_explode and @drawable/ic_dialog_exploded.

Includes can be tricky, but simpler ones which consist of only a few elements and serve a single, precise purpose tend to end up as @layout/loading or @layout/error_message.

I'm still working on naming strings sensibly, but again short, concise names make the whole process a lot easier.

Upvotes: 12

Related Questions