Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Naming Resources

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

What conventions are the most commonly used when naming various resources like colors, drawables and strings and etc?

I have no doubts naming my layouts activity_main or layout_secondary. However, I have always doubts when naming resources mentioned previously. I never know whether I should name these resources after their use or the content. For example:

  • 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?

like image 823
Pijusn Avatar asked Oct 06 '22 11:10

Pijusn


2 Answers

Naming is pretty much personal preference. The name is perfect as long as the name indicates what the functionality of the defined thing is. Also you and any other developer using these definitions should know how what the names mean and which definition to choose. Quite easy if you are consistent with names throughout the project.

For example dark_blue is obviously a blue color while text_highlighted is the color of highlighted text. The name you should use depends on what you need: if you want to classify colors by their name take the first, if you like 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 (text highlight vs text regular etc.) is more important. In this example choosing between text_highlighted and text_regular is a lot more obvious than choosing between color_light_blue and color_dark_blue although they could refer to the same color. The name can help prevent errors.

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 schema is certainly not perfect but the advantage of using prefixes that start with the most generic classification is that you can use code completion to search for specific items step by step. So color_blue_dark could be better than dark_blue_color, at least if you consider the color classification more important than the dark / light classification. The same applies to first_time_prompt. If you have a lot of prompts it would make sense to name them prompt_first_time, promt_other_time, ... If they can be classified by an activity for example that could be used as super category: mainactivity_prompt_*, secondactivity_prompt_* so you know where they belong to.

like image 134
zapl Avatar answered Oct 10 '22 03:10

zapl


Android SDK will be a good place to start for the good practices. You can open up any sample code in the SDK and go through the code and see the variable names.

like image 36
Antrromet Avatar answered Oct 10 '22 03:10

Antrromet