Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 975
Ollie C Avatar asked Jan 21 '11 10:01

Ollie C


People also ask

What is the name of resource file in Android?

The resources locates inside res directory of your app. The Android resource compiler processes resources arcording to which subforder they are in and the format of the file, for example: res/drawable : Contains all resources file that can be drawable, such as (. png, .

How do I manage resources in Android?

Resource Manager is a tool window for importing, creating, managing, and using resources in your app. You can open the tool window by selecting View > Tool Windows > Resource Manager from the menu bar or by selecting Resource Manager on the left side bar. Click Add to add a new resource to your project.

What are the resources in Android programming?

Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize app resources such as images and strings from your code, so that you can maintain them independently.

What is a resource how we access resources in Android?

Accessing Resources When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.


1 Answers

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.

like image 100
Dave Avatar answered Oct 21 '22 05:10

Dave