Anka Wirawan
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Code and Naming Conventions - Android ## 1. Project Stucture New projects should follow the [Android Gradle project structure](http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Project-Structure) that is defined on the Android Gradle plugin user guide. ## 1.1 Naming All source files must be encoded as UTF-8. And resource names following below a simple convention. > ``<what>_<where>_<description>_<size/state>`` Let’s first describe every element briefly. After the advantages, we’ll see how this applies to each resource type. | Keywoard | Description | |----------------- |--------------| | ``<what>`` | Indicate what the resource actually represents, often a standard Android view class. Limited options per resource type. | | ``<where>`` | Describe where it logically belongs in the app or feature. | |``<description>`` | Differentiate multiple elements in one screen. | |``<size/state>`` | The size is based on bucket and the state is based on the event or functionality. Optionally used for drawables, layouts, and dimensions. | ### 1.1.1 Class File Class names are written in [UpperCamelCase](http://en.wikipedia.org/wiki/CamelCase) name of the top-level class, follow below simple convention. > ``<What><Where>`` ``` // SignInActivity.kt class SignInActivity { } ``` ``` // TransferFragment.kt class TransferFragment {} ``` If a source file contains multiple top-level declarations (which should be avoided if possible), choose a name that best describes the contents of the file. ``` // AccountsAdapter.kt fun AccountsEntity.toModel() { /* ... */ } fun AccountsModel.toEntity() { /* ... */ } ``` Model and data classes are exceptions: ``` // Account.kt data class Account { } ``` ### 1.1.2 Resources File Resources file names are written in **lowercase_underscore**. #### 1.1.2.1 Drawable files Naming conventions for drawables: > ``<what>_<descriptions>_<size/state>`` | Asset Type | Prefix | Example | | ------------ | ------------ | -------------------------------- | | Action bar | ab_ | ab_stacked.png | | Button | btn_ | btn_send_pressed.png | | Dialog | dialog_ |dialog_top.png | | Divider | divider_ | divider_horizontal.png | | Icon | ic_ | ic_star.png | | Menu | menu_ | menu_submenu_background.png | | Notification | notification_ | notification_item_background.png | | Tabs | tab_ | tab_regular_pressed.png | #### 1.1.2.2 Icon Files Naming conventions for [icons](http://developer.android.com/design/style/iconography.html): > ``<what>_<descriptions>`` | Asset Type | Prefix | Example | | --------------------------------- | -------------- | --------------------------- | | Icons | ic_ | ic_star.png | | Launcher icons | ic_launcher | ic_launcher_calendar.png | | Menu icons and Action Bar icons | ic_menu | ic_menu_archive.png | | Status bar icons | ic_stat_notify | ic_stat_notify_massage.png | | Tab icons | ic_tab | ic_tab_recent.png | | Dialog icons | ic_dialog | ic_dialog_info.png | #### 1.1.2.3 Font Files Naming conventions for fonts: > ``<what>_<descriptions>`` example: ``arial_italic, tt_common_regular`` #### 1.1.2.4 Anim Files Naming conventions for anims: > ``<what>_<descriptions>`` example: ``anim_fade_in, anime_fade_out`` #### 1.1.2.2 Layout Files Layout files should match the name of the Android components that they are intended for but moving the top level component name to the beginning. The naming follow bellow convention: > ``<what><Where>`` | Component | Class Name | Layout Name | | ---------------- | ------------------------------------ | ------------------------------ | | Activity | UserProfileActivity | activity_user_profile.xml | | Fragment | SignUpFragment | fragment_sign_up.xml | | Dialog | ChangePasswordDialog | dialog_change_password.xml | | AdapterView item | ProductViewHolder | item_product.xml | | Partial layout | Partial layout for DashboardFragment | content_dashboard_favorite.xml | #### 1.1.2.3 Menu Files Menu files should match the name of the component and following by the feature. For example, if we are defining a menu file that is going to be used in the ``UserActivity``, then the name of the file should be ``menu_user.xml`` #### 1.1.2.4 Value Files Resource files in the values folder should be plural, e.g. ``strings.xml``, ``styles.xml``, ``colors.xml``, ``dimens.xml``, ``attrs.xml``. #### 1.1.2.5 Navigation Files Navigation files should match the name of the component and following by the feature, e.g. ``nav_graph_transfer.xml``, ``nav_graph_sign_up.xml``. #### 1.1.2.5 Mipmap Files TBD #### 1.1.2.6 Raw Files TBD #### 1.1.2.7 Transition Files TBD #### 1.1.2.8 XML Files Naming conventions for XML: > ``<what>_<description>`` example: ``lint`` ### 1.1.3 ID or Resources #### 1.1.3.1 ID attribute Naming conventions for ID ``(android:id="@+id/)``: Data Binding: > ``<descriptions>`` Others: > ``<what>_<where>_<descriptions>`` | Widget Type | Type | Prefix | Example | | ---------------- | ------------ | ------- |------------------------ | | All | Data Binding | --- | name, address | | TextView | Others | tv_ | tv_name | | EditText | Others | et_ | et_username | | Linearlayout | Others | ll_ | ll_user_container | | ConstraintLayout | Others | cl_ | cl_account_container | #### 1.1.3.2 Colors Naming conventions for colors: > ``<what><Where><Description><State>`` example: ``colorButtonPrimaryPressed, colorCardViewBorder`` #### 1.1.3.3 Styles Naming conventions for styles: Themes : > ``<What>.<BankMandiri>.<Where>`` Themes for spesific project: > ``<What>.<ProjectName>.<Where>`` Widget: > ``<What>.<BankMandiri>.<Where>.<Descriptions>`` example: ``AppTheme.BankMandiri.Base, AppTheme.Everest.Splash, TextAppearance.BankMandiri.TextView, TextAppearance.Everest.TextView`` #### 1.1.3.4 Strings Naming conventions for strings: > ``<where>_<descriptions>`` example: ``login_username_hint, transfer_amount_hint`` #### 1.1.3.5 Dimens Naming conventions for dimens: Commons: > ``<where>_<description>_<what><size>`` Features: > ``<where>_<what>_<descriptions>_<size>`` example: ``button_height, textview_textsize_small, transfer_account_margin_left`` #### 1.1.4 States Naming conventions for selector states: | State | Suffix | Example | | ---------- | ---------- | ----------------------- | | Normal | _normal | btn_order_normal.png | | Pressed | _pressed | btn_order_pressed.png | | Focused | _focused | btn_order_focused.png | | Disabled | _disabled | btn_order_disabled.png | | Selected | _selected | btn_order_selected.png | #### 1.1.5 Special Characters #### 1.1.5.1 Whitespace characters Aside from the line terminator sequence, the **ASCII horizontal space character** ``(0x20)`` is the only whitespace character that appears anywhere in a source file. This implies that: * All other whitespace characters in string and character literals are escaped. * Tab characters are not used for indentation. #### 1.1.5.2 Special escape sequences For any character that has a special escape sequence (``\b``, ``\n``, ``\r``, ``\t``, ``\'``, ``\"``, ``\\``, and ``\$``), that sequence is used rather than the corresponding Unicode (e.g., ``\u000a``) escape. #### 1.1.5.3 Non-ASCII characters For the remaining non-ASCII characters, either the actual Unicode character (e.g., ``∞``) or the equivalent Unicode escape (e.g., ``\u221e``) is used. The choice depends only on which makes the code easier to read and understand. Unicode escapes are discouraged for printable characters at any location and are strongly discouraged outside of string literals and comments. | Example | Discussion | | --------------------------------- | ------------------------------------------------------------------- | | val unitAbbrev = "μs" | Best: perfectly clear even without a comment | | val unitAbbrev = "\u03bcs" // μs | Poor: there’s no reason to use an escape with a printable character | | val unitAbbrev = "\u03bcs"` | Poor: the reader has no idea what this is | return "\ufeff" + content | Good: use escapes for non-printable characters, and comme | ## 2. Class Structure A kotlin file comprises the following, in order: * Copyright and/or license header (optional) * File-level annotations * Package statement * Import statements * Top-level declarations * Exactly one blank line separates each of these sections. ### 2.1 Copyright / License If a copyright or license header belongs in the file it should be placed at the immediate top in a multi-line comment. ``` /* * Copyright 2019 PT Bank Mandiri (Persero) Tbk. * * ... */ ``` ### 2.2 File-level annotations Annotations with the "file" [use-site target](https://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets) are placed between any header comment and the package declaration. ### 2.3 Package statement The package statement is not subject to any column limit and is never line-wrapped, multiples words concatenated together without underscores or hyphens. ``` com.example.fundstransfer ``` ### 2.3 Import statements Import statements for classes, functions, and properties are grouped together in a single list and ASCII sorted. Wildcard imports (of any type) are **not allowed**, except for data binding. Similar to the package statement, import statements are not subject to a column limit and they are never line-wrapped. ### 2.4 Top-level declarations A kotlin file can declare one or more types, functions, properties, or type aliases at the top-level. The contents of a file should be focused on a single theme. Examples of this would be a single public type or a set of extension functions performing the same operation on multiple receiver types. Unrelated declarations should be separated into their own files and public declarations within a single file should be minimized. No explicit restriction is placed on the number nor order of the contents of a file. Source files are usually read from top-to-bottom meaning that the order, in general, should reflect that the declarations higher up will inform understanding of those farther down. Different files may choose to order their contents differently. Similarly, one file may contain 100 properties, another 10 functions, and yet another a single class. What is important is that each class uses **some** logical order, which its maintainer could explain if asked. For example, new functions are not just habitually added to the end of the class, as that would yield “chronological by date added” ordering, which is not a logical ordering. ### 2.5 Class member ordering The order of members within a class follow the same rules as the top-level declarations. ## 3.1 Formating ### 3.1.1 Kotlin #### 3.1.1.1 Braces Braces are not required for `when` branches and `if` statement bodies which have no `else if/else` branches and which fit on a single line. ``` if (string.isEmpty()) return when (value) { 0 -> return // … } ``` Braces are otherwise required for any `if`, `for`, `when` branch, `do`, and `while` statements, even when the body is empty or contains only a single statement. ``` if (string.isEmpty()) return // WRONG! if (string.isEmpty()) { return // Okay } ``` ##### 3.1.1.1.1 Non-empty blocks Braces follow the `Kernighan and Ritchie` style ("Egyptian brackets") for nonempty blocks and block-like constructs: * No line break before the opening brace. * Line break after the opening brace. * Line break before the closing brace. * Line break after the closing brace, only if that brace terminates a statement or terminates the body of a function, constructor, or named class. For example, there is no line break after the brace if it is followed by `else` or a comma. ``` return Runnable { while (condition()) { foo() } } return object : MyClass() { override fun foo() { if (condition()) { try { something() } catch (e: ProblemException) { recover() } } else if (otherCondition()) { somethingElse() } else { lastThing() } } } ``` A few exceptions for enum classes are given below. ##### 3.1.1.1.2 Empty blocks An empty block or block-like construct must be in `Kernighan and Ritchie` style. ``` try { doSomething() } catch (e: Exception) {} // BAD ``` ``` try { doSomething() } catch (e: Exception) { } // GOOD ``` ##### 3.1.1.1.2 Expressions An `if/else` conditional that is used as an expression may omit braces only if the entire expression fits on one line. ``` val value = if (string.isEmpty()) 0 else 1 // GOOD ``` ``` val value = if (string.isEmpty()) // BAD 0 else 1 ``` ``` val value = if (string.isEmpty()) { // GOOD 0 } else { 1 } ``` #### 3.1.1.2 Indentation Use four(4) sapces for indentations. Each time a new block or block-like construct is opened, the indent increases by four `spaces`. When the block ends, the indent returns to the previous indent level. The indent level applies to both code and comments throughout the block. ``` fun sum(valueA: Int, valueB: Int): Int { return valueA + ValueB } ``` ``` if (elements != null) { for (element in elements) { // ... } } ``` ``` private fun getSharedPreferences() = context.getSharedPreferences(SHARED_PREFERENCES_FILENAME, Context.MODE_PRIVATE) ``` #### 3.1.1.3 One statement per line Each statement is followed by a line break. Semicolons are not used. ``` val total = numberA + numberB val average = total / 2 ... ``` #### 3.1.1.4 Line wrapping Code has a column limit of **120** characters. Except as noted below, any line that would exceed this limit must be line-wrapped, as explained below. Exceptions: * Lines where obeying the column limit is not possible (for example, a long URL in KDoc) * `package` and `import` statements * Command lines in a comment that may be cut-and-pasted into a shell #### 3.1.1.5 Where to break The prime directive of line-wrapping is: prefer to break at a higher syntactic level. Also: * When a line is broken at a non-assignment operator the break comes before the symbol. * This also applies to the following “operator-like” symbols: * The dot separator (`.`). * The two colons of a member reference (`::`). * When a line is broken at an assignment operator the break comes after the symbol. ``` randomStringList .any { !it.isNullOrBlank() } ``` ``` inline fun <reified T: Any> Gson.fromJson(json: JsonElement): T = this.fromJson(json, T ::class.java) ``` ``` val anchor = owner ?.firstChild!! .siblings(forward = true) .dropWhile { it is PsiComment || it is PsiWhiteSpace } ``` ``` val stream = Files.newInputStream(Paths.get("/some/file.txt")) ``` * A method or constructor name stays attached to the open parenthesis (`(`) that follows it. ``` fun drawSquare( x = 10, y = 10, width = 100, height = 100 ) ``` * A comma (`,`) stays attached to the token that precedes it. ``` fun transfer(source: Account, destination: Account, amount: Double, currency: String) ``` * A lambda arrow (`->`) stays attached to the argument list that precedes it. ``` appendCommaSeparated(properties) { prop -> val propertyValue = prop.get(obj) } ``` ### 3.1.2 XML

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully