Android naming convention

I am looking for a detailed suggestion on the Android naming convention. I found a little here:

http://source.android.com/source/code-style.html#follow-field-naming-conventions

which says:

  • Nonpublic, non-static field names begin with m.
  • Static field names begin with s.
  • Other fields begin with a lowercase letter.
  • Public static end fields (constants) are ALL_CAPS_WITH_UNDERSCORES .

However, I am looking for something much more extensive, covering all aspects of Android:

  • what are the layouts and views inside,
  • what to call the menu
  • how to name styles
  • how to name database tables (singular, plural) and fields inside
  • etc

If there is any generally accepted suggestion, I would just like to follow this. All SDKs seem to go their own way, so I'm especially interested in Android's way of doing this.

+67
android naming-conventions
Oct 13 '12 at 6:19 06:19
source share
6 answers

Android reef guidelines are a good example of standard naming conventions:

Naming convention for XML files:

 activity_<ACTIVITY NAME>.xml - for all activities dialog_<DIALOG NAME>.xml - for all custom dialogs row_<LIST_NAME>.xml - for custom row for listview fragment_<FRAGMENT_NAME>.xml - for all fragments 

Naming convention for component / widget in xml files:

All components for action X must begin with an activity name; all components must have a prefix or short name, for example, btn for Button For example, the name for an input activity component should look like this.

 activity_login_btn_login activity_login_et_username activity_login_et_password 

The abbreviated name of the main components:

 Button - btn EditText - et TextView - tv ProgressBar - pb Checkbox - chk RadioButton - rb ToggleButton - tb Spinner - spn Menu - mnu ListView - lv GalleryView - gv LinearLayout -ll RelativeLayout - rl 
+73
Jan 07 '15 at 11:40
source share

This is a great collection of best practices to get you started: https://github.com/futurice/android-best-practices

Here is what I use. I will also copy from this link.

Naming Objects

  • Do not use the m or s prefix as recommended by Google. I have been stopping for many years, and it’s easier for me without them. The IDE will tell you when you use something personal or static; it looks like an outdated convention.
  • CONSTANTS start with caps
  • Acronyms should use only the first letter. For example, functionUrl and unitId . Not unitId .
  • Prefix with object type. For example, a TextView that contains a name will be tvName . EditView with password will be etPass .
  • If this is usually only used once in an activity (e.g. ListView), don't be afraid to just call it lv .
  • If this is not an object type, just call it a function. For example, if it is a string containing an identifier, name it as id , not stringId. The IDE will tell you when it is a string or floating or long.
  • Keep it legible. Use Pass instead of Password .
  • In an XML file, the name should be underlined without capitals, for example. tv_name and et_pass
  • Put android:id as the first attribute in XML.

File naming

  • Prefix layouts with type. For example. fragment_contact_details.xml , view_primary_button.xml , activity_main.xml .
  • For classes, classify them into folders, but use suffixes. For example, /activities/MainActivity.java or /fragments/DeleteDialog.java . My folders are actions, fragments, adapters, models and utilities.
  • Adapters must say how and when they are used. Thus, the ListView adapter for ChatActivity can be called ChatListAdapter .

colors.xml and dens.xml as a palette

  • For color, use names like gray_light , not button_foreground .

  • For sizes, use spacing_large type spacing_large , not button_upper_padding .

  • If you want to set something specific for the color of your button or pad, use a style file.

strings.xml

  • Name your lines keys that resemble namespaces, and don't be afraid to repeat the value for two or more keys.

  • Use error.message.network , not network_error .

Reasoning

The purpose of naming conventions is not to make everything neat and consistent . This is there to identify possible errors and improve the workflow. Most of them are designed for convenient keyboard shortcuts. Try to focus on minimizing errors and improving workflow, not visibility.

The prefixes are great for them: "What is the name of this TextView?" moments.

Suffixes exist for things that you do not access so often, but can be confusing. For example, I'm not sure if I put my code in the Activity, Fragment or Adapter of this page. They can be reset if you wish.

XML identifiers are often lowercase and use underscores just because everyone seems to do it that way.

+17
Dec 23 '15 at 4:47
source share

SEQUENCE
Everyone (if not working in a team) will have their own agreement, and which one you choose does not matter. Make sure that it is consistent throughout the application.


STRUCTURE
Personally, I use a naming convention like this one, since it works from class name to component and is the same for all xml:

  • CLASS : <ClassName>
  • ACTIVITY : <ClassName>**Activity**
  • LAYOUT : classname_activity
  • Component Identifiers : classname_activity_component_name

An example of this would be OrderActivity.class , order_activity.xml , order_activity_bn_cancel . Note that all XML letters are lowercase.


Short layouts
If you want to use shorter names to keep the code clean; then another method may be to reduce ALL XML names as well as layouts.

An example of this would be OrderActivity .class: ord_act .xml, ord_act _bt_can, ord_act _ti_nam, ord_act _tv_nam. I divide the names into three parts, but it depends on how many similar names you have


REDUCED TYPES OF COMPONENTS
When reducing component types, try to keep them consistent as well. I usually use two letters for the type of component and three letters for the name. However, sometimes a name is not required if this is the only element of this type in the layout. The principle of ID must be unique

  • IDS COMPONENT: nam_act_component_nam

ABBREVIATIONS OF COMPONENT TYPE (two letters are shown in this list, which are quite enough)
Frame Structure: fl
Line Layout: ll
Table Location: tl
Table row : tr
Grid Location: gl
Relative layout: RL

View Text: TV
Button: bt
Checkmark: cb
Switch: SW
Toggle Button: tb
Image Button: ib
View Image: iv
Progress Bar: pb
Search bar: Sat
Rating bar: rb
Spinner: sp
WebView: wv
Edit Text: et

Radio Group: rg
Browse List: lv
Type of mesh: G.V.
Expandable Watch List: el
Scroll View: SV
Side Scrolling: hs
Search: * se
Tab Host: th
Watch video: vv
Dialer Filter: df

Include: ic
Fragment: fr.
Custom View (other): cv

+10
Feb 08 '16 at 14:50
source share

I do not think there is an agreement for this. Each company has its own rules, and I do not think that anyone here cares a lot about this.

For me, I prefer to attach the name to the context. for example, if there is an action called "MainActivity", its layout name will be "main_activity.xml", and for each resource associated with this action, I add the prefix "main_activity" so that I know that it uses it. the same applies to identifiers used for this activity.

The reason I use these names is that they are easier to find, delete if necessary, and you cannot replace them with others if you use the Android libraries, as the names are unique.

I also try to give meaningful names as much as possible, so you usually don't see "listView" or "imageView2" as identifiers, but something like "contactsListView" and "contactImageView". the same name (or similar) will also correspond to the variables inside the java code to facilitate their search.

So, in short, my advice is as follows:

  • try to avoid numbers inside names. they usually don't matter much and show that you used drag & drop for the user interface designer.

  • for demos, POC, and for questions here, don’t worry about the name.

  • try adding a prefix to all resource names (including identifiers) to show which context they belong to and achieve uniqueness.

  • give meaningful names where possible.

+8
Oct 13 '12 at 8:01
source share

Each organ uses its own. The main goal is to avoid mistakes and misinterpretations, especially when others are reading your code. Although syntax highlighting and automatic code checking in a modern IDE make it more accurate.

However, these naming conventions are also very handy when you include code. For example, just enter m and auto complete will show you a list of class fields.

But many times you have to work with other code that does not use such an agreement. such protected variables and overridden method parameters simply add to the confusion.

A few examples:

  • Prefix class variables with m and make static final variables all caps, with _ separating words. Do not prefix any thing to reduce region variables.

  • The name layout after the parent user interface, for example act_main.xml , frg_detail.xml , itm__act_main__list1.xml ; for the MainActivity operation, MainActivity fragment, item layout for the ListView in MainActivity with id list1 , respectively.

  • The identifier of the ID element in xml layouts, for example: lsv__act_main__list1 for the ListView and btn__act_main__submit for the Button element. This makes it easy to find them with automatic completion.

+1
Oct 13 '12 at 8:25
source share

The new Android Eclipse plugins create some of the files that you specify automatically when you create a new project. From here naming is this:

 layout/activity_main.xml menu/activity_main.xml ... 

I followed this pattern for example

 layout/fragment_a.xml layout/fragment_b.xml ... 

So, this is something like package names, from general to detailed. It also allows neat sorting.

0
Oct 13 '12 at 8:13
source share



All Articles