Android naming conventions and coding specifications

Source: Internet
Author: User

Concise Summary Multiple Write notes

I. About naming conventions

There must be a unified specification for development projects, but what are some of the naming conventions that need to be done?

A: First, it cannot be anti-human . Come again is, want to look at the text and know its meaning .

Here's how to regulate our code in detail. How to name?

  1) JAva class file

I. activity naming convention: Use activity as a suffix, such as person activity.

Ii. Adapter naming convention: Adapter as a suffix, such as personAdapter.

III. Entity naming conventions: Mostly entity as a suffix, such as personentity. It is important to note that the user is a global variable, not an entity, so you don't have to name it.

  2) resource File

     ①layout directory (all in lowercase ok)

I. page layout file :

Prefix the act_ with the activity's packege as infix, with the name of the activity (remove activity) as the suffix.

such as: act_person_addcoustomer. xml Represents the layout file for the addcustomeractivity under the person module.

II. the item layout file in the ListView :

The name of the list item is suffixed with Item_ as a fixed prefix.

such as: item_lvuserlist. xml means that there is a list of users under a page, and the control is named the layout file for the lvuserlist corresponding item.

III. dialog layout file:

With Dlg_ as the fixed prefix, the function name of dialog is used as a suffix.

such as: dlg_hint. What does XML mean? We all know that.

  Under the ②drawable directory

    The following naming conventions should be adhered to:

I. For resources that are used on only one page , prefix the name of the interface

II. For resources that are used by multiple pages in only one module , prefix the name of the module

III. For resources that may be used in each module or page , such as up navigation, down navigation, common as prefix

3) control objects in Java classes

    The control type abbreviation + logical name of the control (capitalized), such as the login button, can be named Btnlogin.

abbreviations for common controls
Control Abbreviation Control Abbreviation
Layoutview Lv EditText Et
Relativeview Rv Timepicker Tp
TextView Tv ToggleButton Tb
Button Btn Progress Pb
ImageButton Img Wdbview Wv
ImageView iv Rantingbar Rb
CheckBox Chk Tab tab
RadioButton Rb Listview Lv
DatePicker Dp Mapview Mv

4) control objects in layout

Consistent with the name of the control in the activity.

such as: Button Btnlogin = (button) Findviewbyid (R.id.btnlogin);

5) Constants in the String.xml

     such as: Loginactivity_btnlogin_text

Because most of these values are used on controls in layout, they are prefixed with the activity name of the constant, followed by the name of the control, and then free to play back.

The other is using the scenario in Java code,

If it is related to the activity, prefix it with the activity name, as above;

If related to public modules and controls, Common_ is used as a prefix.

A more flexible approach is to split it into multiple strings files by module, as long as the resoures tag is a string tag, and the same files are automatically merged when compiled and packaged.

6) Constant naming

Can only contain letters and underscores _, all uppercase letters, and the words are separated by underscores _ for example: Start_time

Ii. About coding Specifications

1) Separate classes are required to store various classes

Such as:

|-src

|---Com.example.demo

|---activity.others

|---activity.personcenter

|---Adapter

|---db

In turn: Engine, entity, interfaces, listener, UI, Utils ...

2) How do I use the Findviewbyid statement?

3) Constants in layout, to be defined in resource Strings.xml

4) font size of all controls in layout, defined in Dimens.xml

5) in activity, define a new life cycle, thus splitting the OnCreate method into the following 3 parts:

Initvariables: Initialize variables (including data on intent and variables used internally by activity)

Initviews: Loading layout file, initializing control

LoadData: Call Mobileapi

6) Persist in using Fastjson custom entities as MOBILEAPI data carriers

7) Transfer values between pages, insisting on using intent to carry the serialization of entity data. It is forbidden to use global variables for a convenient way to pass values.

8) Add an event to the control, using the. Setonclicklistener ();

9) do not nest internal classes in activity, try to be independent.

10) All adapter are placed in the adapter package; adapter bound data are arraylist< custom serializable entities >; Use the Viewholder entity class in adapter.

11) Entities are not shared between different modules, but can be shared between different pages under the same module.

12) to save memory, use arraylist< custom entity, instead of HashMap

13) Picture processing, use a third-party component Imageloader or Fresco for asynchronous loading

14) When do I use sharedpreference? For simple configuration information, set the various switches on the page, and for complex objects, such as the user class, or to be stored in a local file as well.

15) Try to use ApplicationContext instead of Context, otherwise it will cause a memory leak. Of course, not anywhere applicationcontext can replace the context, improper use will lead to crashes.

16) The data type conversion must be verified.

17) Use constants instead of enumerations. Each value of an enumeration can only be an integer, not a method such as ToString, so it is not as convenient to define a string constant in a class.

          

Android naming conventions and coding specifications

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.