Android Code writing specification

Source: Internet
Author: User
Tags coding standards

1. resource file naming rules
2. Class name file naming conventions
3. Enumerate as few as possible
4.public method, important logic, main class struct must be commented, other parts can be customized annotation
5. The submission code must describe the content of the changes, if one commit too much, split the function to commit multiple times, as far as possible to keep each commit function modified single principle
6. class file As far as possible not more than 300 lines, method as far as possible not more than one screen
7. Do not do the refactoring of too many kinds of files when the function is not completed
8. Adhere to the above 7 points, to become excellent code farmers

############################################################
# Android Coding Standards #
# #
# https://github.com/47deg/coding-guidelines/tree/master/java/android#android-coding-standards #
############################################################

1. Naming conventions

developers should pay special attention to these naming conventions as they differ from those on the standard Java Cod ing conventions.

1.1. Common Resource Files

The folder values would have different files, that would store information for our project. Some of the most common files and their name is

Colors.xml:Colors used in the application
Config.xml:Stores Information To configure we project (ex. keys for services, URLs, etc)
Dimen.xml:Dimensions used in application (ex. Action Bar height, paddings, etc)
strings.xml:Localizable Strings
plurals.xml:Plurals. Contains references to Strings.xml
arrays.xml:Arrays. Contains references to Strings.xml
1.2. Java Packages & Class Names

An Android App should generally follow the following package structure

com.company.product.android
Activities:all activities with the word activity pre-fixed by the activity name: *[name]*activity e.g. mainactivity
Adapters:all adapters with the word Adapter pre-fixed by the Adapter name: *[name]*adapter e.g. userlistadapter
Services:all services including API clients and other persistence related services e.g. UserService
Components:all reusable components utilized in Activity and fragments e.g. Userprofilecomponent
Dialogs:all Fragment dialogs with the word Dialog pre-fixed by the Dialog name: *[name]*dialog e.g. deleteaccountconf Irmationdialog
Fragments:all fragments with the word Fragment pre-fixed by the Fragment name: *[name]*fragment e.g. usermaplocationf Ragment
Utils:all Cross Package utilities with the word utils pre-fixed by the Utility name: *[name]*utils e.g. stringutils
1.3. Resource Names

The following structure should is followed when naming resoures.

Group _ type _ name _ [State] _ [suffix]

group:application area or screen. If The resource is used in different parts of applications ' common ' should are used instead. e.g. Actionbar, menu, Media, popup, footer, audio, etc.
Type:resource Type e.g. background, icon, button, TextField, List, MenuItem, RadioButton, CheckBox, tab, dialog, tit Le, etc.
name:descriptive Name As to what the resource are about. e.g. play, stop.
State : (Optional): The Optional state of a parent resource. e.g. a button could is in ' normal ', ' pressed ', ' disabled ' and ' selected '. A checkbox could is ' on ' or ' off '. These resources should never be used directly on layout but rather as state selectors.
Suffix: (Optional): an arbitrary Suffix that helps to further identify a property of the resource. e.g bright, dark, Opaque, layer.
Below is some examples of properly named resources.

Common_background_app
audio_icon_play_on
common_icon_preferences
actionbar_button_send (XML Resource)
Action_button_send_normal
action_button_send_pressed
action_button_send_disabled
1.4. String Resources

String Resources placed in XML resources files such as Strings.xml, Config. is named following the same conv Ention as Java naming conventions for variables and fields. CamelCase with the first letter lowercased.

Below is some examples of properly named string identifiers.

Serverapiurl
PhoneNumber
Services
URL
1.5. Style Resources

String Resources placed in Styles.xml is named in CamelCase. The following structure should is followed when naming style resoures.

[Group]typename[suffix]

Group (Optional): application area or screen e.g. Actionbar, menu, Media, popup, footer, audio.
Type:resource Type e.g. Background, Icon, Button, Textfield, List, MenuItem, RadioButton, Checkbox, Tab, DIALOG.
name:descriptive Name As to what the resource are about. e.g. play, stop.
Suffix: (Optional): an arbitrary Suffix that helps to further identify a property of the resource. e.g Bright, Dark, Opaque, Layer.
Below is some examples of properly named string identifiers.

Buttonsend
Actionbarbuttonback
ListTitle
1.6. Dimen Resources

dimens Resources placed in Dimen.xml. The following structure should is followed when naming dimentions.

Property _ Default _ Group _ type _ name

Property:type of property Reference. e.g. font_size, padding, margin, height, width.
Default (Optional): Write "Default" If is a general dimen.
Group (Optional): application area or screen e.g. Action_bar, menu, Popup, Wizard.
type (Optional): type of resource. e.g. button, title, text, EditText.
name (Optional): only if is necessary.
Below is some examples.

Padding_default
Font_size_action_bar_button
Height_default_action_bar
We should have some dimension on all projects by default. These is:

Padding_default
Margin_default
Font_size_default_button
Font_size_default_title
Font_size_default_text
Height_default_action_bar
Font_size_default_action_bar
2. Conventions for 7 "Devices

7 "Devices require a special treatment. The problem is this these devices usually use "MDPI" as default density.

these is the consideration to follow when targeting apps in these devices

Copy hdpi drawables to "drawable-sw600dp-mdpi".
Create A new dimensions file for this screens. The file "Dimen.xml" would be in the "values-sw600dp-mdpi" folder. Usually the dimensions is at 150%. All fonts sizes should is in the Dimen.xml file as well.

resource Naming guidelines:
==============================================
drawable:
{Group _ type _ name _ [State] _ [suffix]}
==============================================
ID:
{Group _ UI _ type _ [local] _ name}
==============================================
layout:
{[Group] _ UI _ name}
==============================================
Menu:
{[Group] _ UI _ name}
==============================================
ANIM:
{Group _ name _ [local]}
==============================================
string:
{Group _ [UI] _ name}
==============================================
dimen:
{Group _ name _ property _ [size]}
==============================================
**********************************************
Group:
[Common|uikit|sdk|app]

UI:
[main|kitchen|home|activity|fragment|view|actionbar| ...]

Type:
[BG (background) |ic (icon) |BT (button (must have status)) |txt (TextField)
|list (ListView) |menu (MenuItem) |radio (RadioButton) |checkbox| ...]

Local:
[top|head|bottom|left|right|in|out|rotate| ...]

Property :
[font|padding|margin|height|width| ...]

Size:
[large|big|normal|small|double|treble| ...]

State :
[normal|pressed|disabled|on|off| ...]

suffix:
[light|dark| ...]
==============================================
**********************************************

Android Code writing specification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.