Skip to content

Android Guidelines

Insa Suchantke edited this page Dec 11, 2019 · 6 revisions

File Naming

(Source, access at 04.12.19)

Class files

  • 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)

Drawable files

Naming conventions for drawables

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_

Naming conventions for selector state

State Suffix
Normal _normal
Pressed _pressed
Focused _focused
Disabled _disabled
Selected _selected

Values files

  • Resources file names: lowercase_underscore
  • Ressource files in value folders should be plural strings.xml, styles.xml, dimens.xml, attrs.xml

Layout Files

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

Code Guidelines

XML Style Rules

ID naming

(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

Attribute ordering

(Source, access at 04.12.19) Try to group similar attributes together

  1. View Id
  2. Style
  3. Layout width and height
  4. Other layout attributes, sorted alphabetically
  5. Remaining attributes, sorted alphabetically

Java Style Rules

Naming and fields definition

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

Treat acronyms as words

Good Bad
XMLHttpRequest XMLHTTPRequest
getCustomerId getCustomerID
String url String URL
long id long ID

Use TODO comments

(Source, access at 04.12.19)

  • 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: image and image

Use spaces for identation

(Source, access at 04.12.19)

Use 4 space indents for blocks (never tabs)

image

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

image

Use standard brace style

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

image

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

image

Logging guidelines

  • 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)

Class member ordering

Logical and consistant order

  1. Constants
  2. Fields
  3. Constructors
  4. Override methods and callbacks (public or private)
  5. Public methods
  6. Private methods
  7. Inner class or interfaces

String constants, naming values

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_

Formatting

(Source, access at 04.12.19)

  • Whitespaces: no trailing whitespace on lines; empty lines must not contain whitespaces
  • Spaces vs. tabs: use only spaces

Line length limit

(Source, access at 04.12.19)

  • 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

Clone this wiki locally