Android allows you to keep all text constants in xml files. The same applies to many other things, for example, identifiers. And if your application is a bit more complicated than HelloWorld, then incorrect naming can greatly affect your productivity and the quality of the final product.
While I was programming for Android, I made for myself a couple of rules that slightly improve my life when working with resources (the very magical R):
')
The name of the resource should reflect its essence (the most obvious rule)
For example,
< string name ="set_as_wallpaper" > As wallpaper </ string >
* This source code was highlighted with Source Code Highlighter .
Do not be lazy to rename the automatically generated identifiers, for they only litter the remaining identifiers with almost meaningless names.
A resource name must begin with its main logical domain.
- if this is the title of the title in the menu, then it should start with the menu
- if this is the title of the title in the dialogue, then it should start with the dialog
- if it is a string for toast, then it starts with toast
- if the string is associated with some of your class, then let the name begin with the name of this class
If a string or identifier is strictly related to some context (for example, Activity), then the name of this context should follow the logical name.
For example,
- menu_ main _send (menu item located in activity Main)
- toast_ add_image _success (toast intended for activity AddImage)
Names must begin with the name of the file in which they are located.
For the most part, this refers to the identifiers in the layout. This should be done due to the fact that the framework dumps all identifiers in one pile and if this is not done, it becomes difficult to figure out where the identifier is “physically”.
The name should go through the levels
Those. main logic level, file name, element, string description
- dialog_main_button_search (this applies primarily to the Dialogue. It is located in the Activity Main . This is for the button. This is the inscription search)
- toast_error_while_loading_roller_image (this is primarily the text for the Toast. This is an error. Description
Total
The main benefit of this approach is to quickly search through auto-complete in Eclipse. If, for example, you define a menu in the code, then to find a specific title for an item, just type R.string.menu_ and you will immediately see all the possible names. The same goes for searching the view inside the Activity, just type R.id.my_activity_name and everything is already in full view. Also with this approach, it will be much easier to find where the resource is declared.
If you have any string constants in the code, do not forget that you can always simply transfer Alt + Shift + A quickly into an xml file.