Encoding Specifications for Android

Source: Internet
Author: User
Tags try catch

Android-code-style1. Conventions
    • Activity.oncreate (), fragment.onactivitycreated (), immediately following the member variable, keep the method inside simple and try to call only the Initxxx () method, such as: InitData (), Initview ()
    • The calling method keeps the "proximity principle", the called method, placed below the calling method
    • Single method body not too long
    • Don't misspell words anywhere in the code
    • Unified adjustment of the IDE's tab indent to 4 spaces

      2. Name 2.1. Name of the ID in the layout file

      Rule: Use hump naming, prefix + logical name, class variable name and layout file ID name to be consistent, no underscore split

Control abbreviation prefix
Textview/edittext Text
Button/radiobutton/imagebutton Btn
Relativelayout/linearlayout/framelayout Layout
Listview Listview
WebView WebView
CheckBox CheckBox
ProgressBar ProgressBar
SeekBar SeekBar
Other controls Control name initials as prefix
    • such as: TextView @+id/texttitle
    • such as: EditView @+id/textname
    • such as: Button @+id/btnsearch

      2.2. layout file naming

      Rule: Use the prefix _ logical name, the words are all lowercase, the words are divided by an underscore.

Layout Type Layout prefix
Activity Activity_
Fragment Fragment_
Include Include_
Dialog Dialog_
Popupwindow Popup_
Menu Menu_
Adapter Layout_item_
2.3. resource file naming

Rule: Use prefix _ Use name, the words all lowercase, the words are divided by an underscore.

    • Picture resource file naming
prefix Description
Bg_xxx Various background images
Btn_xxx This button has no other state
Ic_xxxx Icons, typically used for individual icons
Bg_ Description _ Status 1[_ status 2] For different states on a control
Btn_ Description _ Status 1[_ status 2] For different states on a button
Chx_ Description _ Status 1[_ status 2] Selection box, typically 2 states and 4 states
    • Third-party resource files, regardless of the value, drawable
must carry a third-party resource prefix  
Umeng_socialize_style.xml
Pull_refresh_attrs.xml
2.4. Class and interface naming

Rule: Using hump rules, the first letter must be capitalized, using nouns or noun phrases. The requirements are straightforward, descriptive, and do not allow for meaningless or erroneous words.

class Description For example
Application class Application for suffix identification Xxxapplication
Activity class Activity is the suffix identifier Splash screen Page class splashactivity
Parsing classes handler for suffix identification
Public method Classes Utils or manager for suffix identification
Thread Pool Management Classes Threadpoolmanager
Log Tool class Logutils
Database classes Identified by DBHelper suffix Mysqlitedbhelper
Service class Identified as a service suffix Playback service: Playservice
Broadcastreceiver class The suffix is identified by broadcast Time Notice: timebroadcast
ContentProvider class The suffix is identified by provider Word content provider Dictprovider
Shared base class for direct write Prefixed with base Baseactivity,basefragment
2.5. Naming the method

Rule: Using hump rules, the first letter must be lowercase, using verbs. The requirements are straightforward, descriptive, and do not allow for meaningless or erroneous words.

Method Description
INITXX () Initialize the associated method, using init as the prefix identifier, such as Initialize Layout Initview ()
Httpxx () HTTP business request method, identified by HTTP prefix
GETXX () A method that returns a value that is identified with a get prefix
Savexx () Associated with saving data, use Save as the prefix to identify
Deletexx () Delete operation
RESETXX () For data reorganization, use the reset prefix to identify
Clearxx () Clear data-related
ISXX () Method returns a Boolean value using is or check for prefix identification
PROCESSXX () Methods of processing data, using process as a prefix to identify
Displayxx () Pop-up tips and hints, using display as a prefix to identify
Drawxxx () Drawing data or effects related, using the draw prefix to identify
2.6. Variable naming

Rule: Using hump rules, the first letter must be lowercase, using nouns or noun phrases. The requirements are straightforward, descriptive, and do not allow for meaningless or erroneous words.

    • Member variable name, add m prefix before custom variable, layout control variable does not add m prefix
    • Constant name, all uppercase, and between words with underscores

      3. Other specifications
    • Activity inherits Basefragmentactivity or swipebackactivity, you can use Butterknife annotations instead of Findviewbyid
    • Method
    1. Split the bloated method, one thing for each method
    2. Do the same logical approach, as close as possible to a piece, for easy viewing
    3. Do not use try catch to process business logic
    4. Do not manually parse and assemble data using the JSON tool class
    • Control statements
    1. Reduce conditional nesting, no more than 3 layers
    2. If Judge uses "Guardian statement", reduce the level
      if (obj! = null) {dosomething ();}
      Modified to:
      if (obj = = null) {return;} dosomething ();
    3. The IF statement must be included with {}, even if there is only one sentence.
    • Dealing with mysterious numbers like "magic number" and so on.
    1. Don't show numbers in your code, especially some that identify different types of numbers.
    2. All meaning figures are extracted into the constant public class, and are not scattered among the classes.
    • Blank line: Empty lines are separated by logical related code snippets, concise and clear, improve readability
    1. Between member variables, grouped by business formation, plus empty lines
    2. Add blank lines between methods
    • Use a good todo tag
        1. Record ideas, record function points, use Todo to record temporary ideas during development, or leave a perfect explanation for not disturbing ideas
        2. Delete useless todo, development tool automatically generated TODO, or already perfect todo, be sure to delete.

Encoding Specifications for Android

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.