Android: naming resources

This is a question for Android developers, but it is not a programming issue as it affects nothing but the developer.

What conventions are most often used when naming various resources, such as colors, drawings and strings, etc.?

I have no doubt about the name of my activity_main or layout_secondary . However, I always doubt when the previously mentioned resources were mentioned. I never know if I should specify these resources after using them or their contents. For instance:

  • Color: dark_blue vs text_highlighted
  • Drawable: blue_gradient vs top_bar_background
  • String: welcome_to_app vs first_time_prompt

Is there any community-created resource for good practice?

+4
source share
3 answers

Naming is a personal preference. A name is ideal if the name indicates what the functionality of a certain thing is. You and any other developer using these definitions should also know what the names mean and which definition to choose. Pretty easy if you agree with the names throughout the project.

For example, dark_blue is obviously blue, and text_highlighted is the color of the selected text. The name that you should use depends on what you need: if you want to classify colors by their name, take the first, if you want to abstract from the actual color, take the second. For general layouts using text_highlighted will often make more sense, since the actual color does not matter, and the functionality is more important (text highlighting and regular text, etc.). In this example, the choice between text_highlighted and text_regular much more obvious than the choice between color_light_blue and color_dark_blue , although they may all refer to the same color. A name can help prevent mistakes.

Android uses prefixes for names in [ android.R.drawable ] (http://developer.android.com/reference/android/R.drawable.html), for example:

  • btn_ for button graphics
  • ic_ for icon graphics
    • ic_menu_ for menu icons
    • ic_dialog_ for dialog icons
  • stat_ for status icons

The scheme, of course, is not perfect, but the advantage of using prefixes that begin with the most general classification is that you can use code completion to search for specific elements step by step. Thus, color_blue_dark may be better than dark_blue_color , at least if you think the color classification is more important than the dark / light classification. The same applies to first_time_prompt . If you have a lot of prompt , it makes sense to call them prompt_first_time , promt_other_time , ... if they can be classified by activity, for example, which can be used as a supercategory: mainactivity_prompt_* , secondactivity_prompt_* , so that you know what they belong to.

+2
source

The Android SDK is a good place to start practicing. You can open any sample code in the SDK and go through the code and see the variable names.

+1
source

I usually name assets, such as colors and images for their contents, but I will name a style or several states that can be used for this function.

for example: button_On.png; button_Off.png; button.xml button_On.png; button_Off.png; button.xml

Thus, if I want to use the same resource in several places, it is not confused. For example, using color as the color of text in one style file and background in another style file.

+1
source

Source: https://habr.com/ru/post/1437177/


All Articles