I am looking for a thorough Android naming convention suggestion. I found a little bit here:
http://source.android.com/source/code-style.html#follow-field-naming-conventions
which says:
ALL_CAPS_WITH_UNDERSCORES
.Yet I am looking for something much more extensive covering all aspects of Android:
If there is some generally accepted suggestion I would just love to follow that. All SDKs seem to go their own way so I am particular interested in the Android way to do it.
Each new version of Android is named after a dessert, going in alphabetical order (Fig. 1.5). Fig. 1.5 | Android version numbers and the corresponding names.
Google operating systems are always named after a sweet, like Cupcake, Donut, KitKat or Nougat. A Google spokesperson once said, "Android powers over one billion smartphones and tablets. Since these devices make our lives so sweet, each Android version is named after a dessert".
Project naming conventions include a standard format for the name and unique identification of each project, and standard identification for each project deliverable. The naming conventions also include version control attributes for both project management and software development (engineering) documentation.
ribot's Android 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 X activity must start with the activity name
all component should have prefix or short name like btn for Button
For example,name for login activity component should be like following.
activity_login_btn_login
activity_login_et_username
activity_login_et_password
Short name of major 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
This is an excellent collection of best practices to start with: https://github.com/futurice/android-best-practices
Here's what I use. I'll also copy from that link.
m
or s
prefix as per Google guidelines. I've stopped for years and I find it easier without them. The IDE will tell you when you're using something private or static; it seems like an obsolete convention.functionUrl
and unitId
. Not unitID
.tvName
. An EditView with a password would be etPass
.lv
.id
, not stringId. The IDE will tell you when it's a string or a float or a long.Pass
instead of Password
.tv_name
and et_pass
android:id
as the first attribute in the XML.fragment_contact_details.xml
, view_primary_button.xml
, activity_main.xml
./activities/MainActivity.java
or /fragments/DeleteDialog.java
. My folders are activities, fragments, adapters, models, and utils.ChatListAdapter
.For color, use names like gray_light
, not button_foreground
.
For dimens, use names like spacing_large
, not button_upper_padding
.
If you want to set something specific for your button color or padding, use a style file.
Name your strings with keys that resemble namespaces, and don't be afraid of repeating a value for two or more keys.
Use error.message.network
, not network_error
.
The purpose of naming conventions is not to make everything neat and consistent. It's there to flag possible mistakes and improve workflow. Most of these are designed to be convenient for keyboard shortcuts. Try to focus around minimizing bugs and improving workflow rather than looking nice.
Prefixes are great for those, "What's the name of that TextView?" moments.
Suffixes are there for the things which you don't access so often in that manner, but can be confusing. For example, I may not be sure whether I put my code in the Activity, Fragment, or Adapter of that page. They can be dropped if you like.
XML ids are often in lowercase and uses underscores just because everyone seems to do it this way.
CONSISTENCY
Everyone (unless working in teams) will have their own convention and which one you choose does not matter. Making sure it is consistent throughout the whole application does matter.
STRUCTURE
Personally, I use a naming convention like this as it runs from the class name down to component and is consistent throughout the xml:
<ClassName>
<ClassName>**Activity**
classname_activity
classname_activity_component_name
An example of this would be OrderActivity.class
, order_activity.xml
, order_activity_bn_cancel
. Notice all the XML is in lowercase.
ABBREVIATING LAYOUTS
If you would like to use shorter names to keep the code tidier; then another method can be to abbreviate ALL the names in XML aswell as the 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 break down the names into three but this depends how many similar names you have
ABBREVIATING COMPONENT TYPES
When abbreviating component types try to keep these consistent too. I normally use two letters for the component type and three letters for the name. However sometimes the name will not be necessary if that is the only element of that type in the layout. The principle of the ID is to be unique
nam_act_component_nam
COMPONENT TYPE ABBREVIATIONS (This list shows two letters which is plenty)
Frame Layout: fl
Linear Layout: ll
Table Layout: tl
Table Row: tr
Grid Layout: gl
Relative Layout: rl
Text View: tv
Button: bt
Check Box: cb
Switch: sw
Toggle Button: tb
Image Button: ib
Image View: iv
Progress Bar: pb
Seek Bar: sb
Rating Bar: rb
Spinner: sp
WebView: wv
Edit Text: et
Radio Group: rg
List View: lv
Grid View: gv
Expandable List View: el
Scroll View: sv
Horizontal Scroll View: hs
Search View:* se
Tab Host: th
Video View: vv
Dialer Filter: df
Include: ic
Fragment: fr
Custom View (other): cv
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With