-
Notifications
You must be signed in to change notification settings - Fork 0
Android Guidelines
Insa Suchantke edited this page Dec 11, 2019
·
6 revisions
- Class names are written in UpperCamelCase
- Classes that extend an Android component, the name of the class should end with name of component (e.g. SignInActivity, SignInFragment, ImageUploaderService, ChangePasswordDialog)
| Type | Prefix |
|---|---|
| Button | btn_ |
| Text View | tv_ |
| Card View | cv_ |
| List View | lv_ |
| Image View | img_ |
| Edit Text | et_ |
| Check Box | chk_ |
| Action Bar | ab_ |
| Progress Bar | pb_ |
| Dialog | dialog_ |
| Menu | menu_ |
| Notification | notification_ |
| Radio Button | rb_ |
| Toggle Button | tb_ |
| Icon | ic_ |
| Launcher Icon | ic_launcher_ |
| Menu & Action Bar Icon | ic_menu_ |
| Status Bar Icon | ic_stat_notify_ |
| Tab Icon | ic_tab_ |
| Dialog Icon | ic_dialog_ |
| State | Suffix |
|---|---|
| Normal | _normal |
| Pressed | _pressed |
| Focused | _focused |
| Disabled | _disabled |
| Selected | _selected |
- Resources file names: lowercase_underscore
- Ressource files in value folders should be plural strings.xml, styles.xml, dimens.xml, attrs.xml
| Component | Class Names | Layout Name |
|---|---|---|
| Activity | UserProfileActivity | activity_user_profile |
| Fragment | SignUpFragment | fragment_sign_up |
| Dialog | ChangePasswordDialog | dialog_change_password |
| Adapter View Item | --- | item_personal |
| Partial Layout | --- | partial_stats_bar |
(Source, access at 04.12.19) IDs should be prefixed with the name of the element in lowercase underscore
| WHAT | WHERE | DESCRIPTION |
|---|---|---|
| name of Android/Custom view class (e.g. Button --> btn_) | activity separated without activity_ (e.g. activity_select_measurement_input --> _ select_measurement_input_) | differentiate multiple elements in one screen (e.g. is a Button for saving --> _save) |
--> btn_select_measurement_input_save
(Source, access at 04.12.19) Try to group similar attributes together
- View Id
- Style
- Layout width and height
- Other layout attributes, sorted alphabetically
- Remaining attributes, sorted alphabetically
Fields should be defined at the top of the file
- Private, non-static field names start with m
- Private, static field names start with s
- Other fields start with a lower case letter
- Static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES
- Struct/union field names use lowerCamelCase
- Interface names must start with an I followed by an UpperCamleCase / PascalCase name
- Function names, arguments, return variables use lowerCamelCase
| Good | Bad |
|---|---|
| XMLHttpRequest | XMLHTTPRequest |
| getCustomerId | getCustomerID |
| String url | String URL |
| long id | long ID |
- For code that is temporary, a short-term solution, or good-enough but not perfect
- Should include the tring TODO in all caps, followed by a colon:
and
Use 4 space indents for blocks (never tabs)

Use 8 space indents for line wraps, including function calls and assignments

Braces go on the same line as the code before them, they don’t go on their own line

Braces around the statements are required unless the condition and the body fit on one line

- Log.v(String tag, String msg) (verbose)
- Log.d(String tag, String msg) (debug)
- Log.i(String tag, String msg) (information)
- Log.w(String tag, String msg) (warning)
- Log.e(String tag, String msg) (error)
Logical and consistant order
- Constants
- Fields
- Constructors
- Override methods and callbacks (public or private)
- Public methods
- Private methods
- Inner class or interfaces
When using one of the these components, you must define the keys as a statical final fields and they should be prefixed as indicated below.
| Element | Field Name Prefix |
|---|---|
| SharedPreferences | PREF_ |
| Bundle | BUNDLE_ |
| Fragment Arguments | ARGUMENT_ |
| Intent Extra | EXTRA_ |
| Intent Action | ACTION_ |
- Whitespaces: no trailing whitespace on lines; empty lines must not contain whitespaces
- Spaces vs. tabs: use only spaces
- Not exceed 100 characters
- If line is longer than this limit there are usually two options to reduce ist length: ** Extract a local variable or method (preferable) ** Apply line-wrapping to divide a single line into multiple ones
- There are two exceptions where it is possible to have lines longer than 100: ** Lines that are not possible to split, e.g. long URLs in comments ** package and import statements