Code guidelines used when developing Android applications.
Class names are in UpperCamelCase.
| Component | Class Name |
|---|---|
| Activity | LoginActivity |
| Fragment | ProfileFragment |
| Service | BroadcastService |
| Adapter | ContactAdapter |
| Holder | ContactHolder |
| Dialog | EditTextDialog |
Resource names are in lowercase_underscore.
Layout names should specify the component type follow by the class name defined.
| Component | Class Name | Layout Name |
|---|---|---|
| Activity | LoginActivity | activity_login |
| Fragment | ProfileFragment | fragment_profile |
| Adapter | -- | item_contact |
| Dialog | EditTextDialog | dialog_edit_text |
String names should specify the type of string follow by the use of string.
| Prefix | Description |
|---|---|
| error_ | Error message |
| msg_ | Regular information message |
| title_ | Title e.g. Activity or dialog title |
| action_ | An action such as save, cancel, ok etc. |
Drawable names should specify the type of drawable follow by the use of drawable.
| Drawable Type | Prefix | Drawable Name |
|---|---|---|
| Background image (png, jpeg) | bg_ | bg_article |
| Button with background | btn_bg_ | btn_bg_purple |
| Button with border | btn_bd_ | btn_bd_purple |
| Button with radius 3 | btn_rd_ | btn_rd_3 |
| Button with background, border and radius | btn_bg_bd_rd | btn_bg_white_bd_purple_rd |
| Icon | ic_ | ic_launcher |
ID names should specify type of element follow by use of the content.
| Element | Prefix |
|---|---|
| Layouts (e.g. LinearLayout) | layout_ |
| TextView | text_ |
| EditText | edit_ |
| ImageView | image_ |
| Button | button_ |
| FloatingActionButton | fab_ |
| ProgressBar | progress_ |
- Constants (static final fields) are ALL_CAPS_WITH_UNDERSCORES.
- Private/protected, non-static field names start with m.
- Private/protected, static field names start with s.
- Other fields start with a lower case letter.
public class MainActivity {
public static final String TITLE_DIALOG = "title";
public int id;
private static int sVariable;
private int mVariable;
}| Element | Prefix |
|---|---|
| SharedPreferences | PREF_ |
| Bundle | BUNDLE_ |
| Intent Extra | EXTRA_ |
private static final String PREF_EMAIL = "PREF_EMAIL";
private static final String BUNDLE_EMAIL = "BUNDLE_EMAIL";
private static final String EXTRA_EMAIL = "com.myapp.extras.EXTRA_EMAIL";Braces shall be on the same line as the code before them.
if(isTrue) {
// ...
} else {
// ...
}Use braces even if the if condition and body fit on one line!
if(isTrue) {
// ...
}- Constants
- UI variables (TextView etc)
- Other variables (String, int)
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
private EditText mEditTextEmail;
private String mEmail;
}- Constructors
- Override methods and callbacks (public or private)
- Public methods
- Private methods
- Inner classes or interfaces
Each code line should not exceed 100 characters.
Picasso.with(context)
.load("http://google.com/hello.jpg")
.into(imageView);loadPicture(context,
"http://google.com/hello.jpg",
mImageViewProfilePicture,
clickListener,
"Title of the picture");Use Log to print out error messages or other information that may be useful for developers to identify issues. VERBOSE and DEBUG logs must be disabled on release builds. Leave INFORMATION, WARNING and ERROR logs enabled only if they are not leaking private information such as email addresses.
To only show logs on debug builds:
if (BuildConfig.DEBUG) {
Log.d(TAG, "Email: " + email);
}Show all log messages (default).
Log.v(String tag, String msg)Show debug log messages that are useful during development, such as logging the exact flow of your program.
Log.d(String tag, String msg)Show expected log messages for regular usage, as well as the message levels lower in this list.
Log.i(String tag, String msg)Show possible issues that are not yet errors.
Log.i(String tag, String msg)Show issues that have caused errors, such as in a catch statement.
Log.e(String tag, String msg)Use class name as tag and define it as a static final field at the top of the file. For example:
public class MainActivity {
private static final String TAG = MainActivity.class.getSimpleName();
public method() {
Log.e(TAG, "Error message");
}
}