New projects should follow the Android Gradle project structure that is defined on the Android Gradle plugin user guide. The ribot Boilerplate project is a good reference to start from.
Class names are written in UpperCamelCase.
For classes that extend an Android component, the name of the class should end with the name of the component; for example: SignInActivity
, SignInFragment
, ImageUploaderService
, ChangePasswordDialog
.
Resources file names are written in lowercase_underscore.
Naming conventions for drawables:
Asset Type | Prefix | Example |
---|---|---|
Action bar | ab_ |
ab_stacked.9.png |
Button | btn_ |
btn_send_pressed.9.png |
Dialog | dialog_ |
dialog_top.9.png |
Divider | divider_ |
divider_horizontal.9.png |
Icon | ic_ |
ic_star.png |
Menu | menu_ |
menu_submenu_bg.9.png |
Notification | notification_ |
notification_bg.9.png |
Tabs | tab_ |
tab_pressed.9.png |
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_msg.png |
Tab icons | ic_tab |
ic_tab_recent.png |
Dialog icons | ic_dialog |
ic_dialog_info.png |
Naming conventions for selector states:
State | Suffix | Example |
---|---|---|
Normal | _normal |
btn_order_normal.9.png |
Pressed | _pressed |
btn_order_pressed.9.png |
Focused | _focused |
btn_order_focused.9.png |
Disabled | _disabled |
btn_order_disabled.9.png |
Selected | _selected |
btn_order_selected.9.png |
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. For example, if we are creating a layout for the SignInActivity
, the name of the layout file should be activity_sign_in.xml
.
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 | --- | item_person.xml |
View / ViewGroup | SliderView |
view_slider.xml |
Partial layout | --- | partial_stats_bar.xml |
A slightly different case is when we are creating a layout that is going to be inflated by an Adapter
, e.g to populate a ListView
. In this case, the name of the layout should start with item_
.
Other case is when layout is intended to be inflated as View or ViewGroup custom implementation layout. In this case view_
prefix should be used.
Note that there are cases where these rules will not be possible to apply. For example, when creating layout files that are intended to be part of other layouts. In this case you should use the prefix partial_
.
Similar to layout files, menu files should match the name of the component. 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 activity_user.xml
A good practice is to not include the word menu
as part of the name because these files are already located in the menu
directory.
Resource files in the values folder should be plural, e.g. strings.xml
, styles.xml
, colors.xml
, dimens.xml
, attrs.xml
You must never do the following:
void setServerPort(String value) {
try {
serverPort = Integer.parseInt(value);
} catch (NumberFormatException e) { }
}
While you may think that your code will never encounter this error condition or that it is not important to handle it, ignoring exceptions like above creates mines in your code for someone else to trip over some day. You must handle every Exception in your code in some principled way. The specific handling varies depending on the case. - (Android code style guidelines)
See alternatives here.
You should not do this:
try {
someComplicatedIOFunction(); // may throw IOException
someComplicatedParsingFunction(); // may throw ParsingException
someComplicatedSecurityFunction(); // may throw SecurityException
// phew, made it all the way
} catch (Exception e) { // I'll just catch all exceptions
handleError(); // with one generic handler!
}
See the reason why and some alternatives here. If you still decide to do this, write a comment why this is a good idea.
We don't use finalizers. Read more: Android code style guidelines
This is bad: import foo.*;
This is good: import foo.Bar;
See more info here.
Serialization and deserialization of objects is a CPU-intensive procedure and is likely to slow down your application. While performance loss won't be visible on small objects beware of serialization and deserialization of large lists of objects. Preferable approach is to use Parcelable.
Synchronization has hight performance cost in java and improper synchronization can also cause a deadlock. So use synchronization if you are truly dealing with multithreaded processes and doing so follow the guidelines. You can read more detailed guidelines here: Java Language Best Practices
If only certain operations in the method must be synchronized, use a synchronized block with a lock instead of synchronizing the entire method. For example:
private final Object lock = new Object();
...
private void doSomething()
{
// perform tasks that do not require synchronicity
...
synchronized (lock)
{
...
}
...
}
Some Java objects (such as Hashtable, Vector, and StringBuffer) already have synchronization built into many of their APIs. They may not require additional synchronization.
Some Java variables and operations are not atomic. If these variables or operations can be used by multiple threads, you must use synchronization to prevent data corruption. For example: (i) Java types long and double are comprised of eight bytes; any access to these fields must be synchronized. (ii) Operations such as ++ and -- must be synchronized because they represent a read and a write, not an atomic operation.
Fields should be defined at the top of the file and they should follow the naming rules listed below.
- Static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
- Other fields start with a lower case letter.
Example:
public class MyClass {
public static final int SOME_CONSTANT = 42;
private static MyClass myClass;
public int publicField;
int packagePrivateField;
private int privateField;
protected int protectedField;
}
Good | Bad |
---|---|
XmlHttpRequest |
XMLHTTPRequest |
getCustomerId |
getCustomerID |
String url |
String URL |
long id |
long ID |
Use 4 space indents for blocks:
if (x == 1) {
x++;
}
Use 8 space indents for line wraps:
Instrument i =
someLongExpression(that, wouldNotFit, on, one, line);
Braces go on the same line as the code before them.
class MyClass {
int func() {
if (something) {
// ...
} else if (somethingElse) {
// ...
} else {
// ...
}
}
}
Braces around the statements are required unless the condition and the body fit on one line.
If the condition and the body fit on one line and that line is shorter than the max line length, then braces are not required, e.g.
if (condition) body();
This is bad:
if (condition)
body(); // bad!
According to the Android code style guide, the standard practices for some of the predefined annotations in Java are:
-
@Override
: The @Override annotation must be used whenever a method overrides the declaration or implementation from a super-class. For example, if you use the@inheritDoc
Javadoc tag, and derive from a class (not an interface), you must also annotate that the method@Override
s the parent class's method. -
@SuppressWarnings
: The@SuppressWarnings
annotation should only be used under circumstances where it is impossible to eliminate a warning. If a warning passes this "impossible to eliminate" test, the@SuppressWarnings
annotation must be used, so as to ensure that all warnings reflect actual problems in the code.
More information about annotation guidelines can be found here.
Classes, Methods and Constructors
When annotations are applied to a class, method, or constructor, they are listed after the documentation block and should appear as one annotation per line .
/* This is the documentation block about the class */
@AnnotationA
@AnnotationB
public class MyAnnotatedClass { }
Fields
Annotations applying to fields should be listed on the same line, unless the line reaches the maximum line length.
@Nullable @Mock DataManager mDataManager;
The scope of local variables should be kept to a minimum (Effective Java Item 29). By doing so, you increase the readability and maintainability of your code and reduce the likelihood of error. Each variable should be declared in the innermost block that encloses all uses of the variable.
Local variables should be declared at the point they are first used. Nearly every local variable declaration should contain an initializer. If you don't yet have enough information to initialize a variable sensibly, you should postpone the declaration until you do. - (Android code style guidelines)
VERBOSE and DEBUG logs must be disabled on release builds. It is also recommended to disable INFORMATION, WARNING and ERROR logs but you may want to keep them enabled if you think they may be useful to identify issues on release builds. If you decide to leave them enabled, you have to make sure that they are not leaking private information such as email addresses, user ids, etc.
There is no single correct solution for this but using a logical and consistent order will improve code learnability and readability. It is recommendable to use the following order:
- Inner interfaces
- Constants
- Static fields
- Static methods
- Fields
- Constructors
- Overridden methods and callbacks
- Public methods
- Private methods
- Inner classes
Example:
public class MainFragment extends Fragment {
public interface Callback {
void onPhotoSent();
}
public static final String TAG = "MainFragment";
private static final int DEFAULT_TIMEOUT = 20;
public static String publicStaticString;
private static String privateStaticString;
public static Fragment newInstance() {
return new MainFragment();
}
private String title;
private TextView textView;
public void setTitle(String title) {
this.title = title;
}
@Override
public void onCreate() {
...
}
private void setUpView() {
...
}
static class AnInnerClass {
...
}
}
If your class is extending an Android component such as an Activity or a Fragment, it is a good practice to order the override methods so that they match the component's lifecycle. For example, if you have an Activity that implements onCreate()
, onDestroy()
, onPause()
and onResume()
, then the correct order is:
public class MainActivity extends Activity {
//Order matches Activity lifecycle
@Override
public void onCreate() {}
@Override
public void onResume() {}
@Override
public void onPause() {}
@Override
public void onDestroy() {}
}
When programming for Android, it is quite common to define methods that take a Context
. If you are writing a method like this, then the Context must be the first parameter.
The opposite case are callback interfaces that should always be the last parameter.
Examples:
// Context always goes first
public User loadUser(
Context context,
int userId
);
// Callbacks always go last
public void loadUserAsync(
Context context,
int userId,
UserCallback callback
);
Many elements of the Android SDK such as SharedPreferences
, Bundle
, or Intent
use a key-value pair approach so it's very likely that even for a small app you end up having to write a lot of String constants.
When using one of these components, you must define the keys as a static final
fields and they should be prefixed as indicated below.
Element | Field Name Prefix |
---|---|
SharedPreferences | PREF_ |
Bundle | BUNDLE_ |
Fragment Arguments | ARG_ |
Intent Extra | EXTRA_ |
Intent Action | ACTION_ |
Activity result extras | RESULT_ |
Note that the arguments of a Fragment - Fragment.getArguments()
- are also a Bundle. However, because this is a quite common use of Bundles, we define a different prefix for them.
Also use RESULT_
prefix for Activity result intents.
Example:
// Note the value of the field is the same as the name to avoid duplication issues
static final String PREF_EMAIL = "PREF_EMAIL";
static final String BUNDLE_AGE = "BUNDLE_AGE";
static final String ARG_USER_ID = "ARG_USER_ID";
static final String EXTRA_SURNAME = "EXTRA_SURNAME";
static final String RESULT_SURNAME = "RESULT_SURNAME";
// Intent action keys use full package name as value
static final String ACTION_OPEN_USER = "com.myapp.action.ACTION_OPEN_USER";
When data is passed into an Activity
or Fragment
via an Intent
or a Bundle
, the keys for the different values must follow the rules described in the section above.
When an Activity
or Fragment
expects arguments, it should provide a public static
method that facilitates the creation of the relevant Intent
or Fragment
.
In the case of Activities the method is usually called getStartIntent()
:
public static Intent getStartIntent(Context context, User user) {
Intent intent = new Intent(context, ThisActivity.class);
intent.putParcelableExtra(EXTRA_USER, user);
return intent;
}
For retrieving extras use getter with 'Extra' prefix:
private User getExtraUser() {
return getIntent().getParcelableExtra(EXTRA_USER);
}
For Fragments it is named newInstance()
and handles the creation of the Fragment with the right arguments:
public static UserFragment newInstance(User user) {
UserFragment fragment = new UserFragment;
Bundle args = new Bundle();
args.putParcelable(ARG_USER, user);
fragment.setArguments(args)
return fragment;
}
Note: If we provide the methods described above, the keys for extras and arguments should be private
because there is not need for them to be exposed outside the class.
For retrieving fragment arguments use getter method with 'Arg' prefix:
private User getArgUser() {
// No need for getArguments() == null check as long as
// there is no newInstance method with zero args.
return getArguments().getParcelable(ARG_USER);
}
It is very common to instantiate views in Fragment's onCreateView()
method and declare them as class variables. But the naming of these variable could be tricky in some cases.
For example:
// BAD EXAMPLE!
// The view nameText and a String can be confused with each other.
private String name;
private TextView nameText;
To avoid this confusion and to group different views by their type a simple rule should be applied: View variable name should be prefixed with its type shortening. For example:
// tv stands for TextView
private TextView tvName, tvEmail;
// iv stands for ImageView
private ImageView ivLogo;
// b stands for Button
private Button bSubmit;
// w stands for Wrapper
private LinearLayout wUserData
Views should be declared in single line declaration.
Bad:
private TextView tvName;
private TextView tvEmail;
Good:
private TextView tvName, tvEmail;
Clicks or events from the UI that are passed to the view model must call a function prefixed with "on", e.g. onOkClicked().
Events emitted back to the UI from the view model must be Flow (with Channel) or SingleLiveEvent and have the "on" prefix, e.g. onError.
State emitted to the UI should indicate what data it is showing, e.g. "loading" that shows a progress indicator.
fun onOkClicked(): Unit
val onEventHappened = SingleLiveEvent<Unit>()
val onError = SingleLiveEvent<Error>()
val loading = NonNullMutableLiveData(false)
private val onErrorChannel = Channel<String>(Channel.BUFFERED)
val onError = onErrorChannel.receiveAsFlow()
Code lines should not exceed 100 characters. If the line is longer than this limit there are usually two options to reduce its 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
andimport
statements.
There isn't an exact formula that explains how to line-wrap and quite often different solutions are valid. However there are a few rules that can be applied to common cases.
Break at operators
When the line is broken at an operator, the break comes before the operator. For example:
int longName = anotherVeryLongVariable + anEvenLongerOne - thisRidiculousLongOne
+ theFinalOne;
Assignment Operator Exception
An exception to the break at operators
rule is the assignment operator =
, where the line break should happen after the operator.
int longName =
anotherVeryLongVariable + anEvenLongerOne - thisRidiculousLongOne + theFinalOne;
Method chain case
When multiple methods are chained in the same line - for example when using Builders - every call to a method should go in its own line, breaking the line before the .
Picasso.with(context).load("http://ribot.co.uk/images/sexyjoe.jpg").into(imageView);
Picasso.with(context)
.load("http://ribot.co.uk/images/sexyjoe.jpg")
.into(imageView);
Multiple parameters case
When a method has more than one parameter or its parameter is very long, we should break the line after (
, before )
, and after every comma ,
.
loadPicture(context, "http://ribot.co.uk/images/sexyjoe.jpg", mImageViewProfilePicture, clickListener, "Title of the picture");
loadPicture(
context,
"http://ribot.co.uk/images/sexyjoe.jpg",
mImageViewProfilePicture,
clickListener,
"Title of the picture"
);
Rx chains of operators require line-wrapping. Every operator must go in a new line and the line should be broken before the .
public Observable<Location> syncLocations() {
return mDatabaseHelper.getAllLocations()
.concatMap(new Func1<Location, Observable<? extends Location>>() {
@Override
public Observable<? extends Location> call(Location location) {
return mRetrofitService.getLocation(location.id);
}
})
.retry(new Func2<Integer, Throwable, Boolean>() {
@Override
public Boolean call(Integer numRetries, Throwable throwable) {
return throwable instanceof RetrofitError;
}
});
}
When an XML element doesn't have any contents, you must use self closing tags.
This is good:
<TextView
android:id="@+id/tvViewProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This is bad :
<!-- Don\'t do this! -->
<TextView
android:id="@+id/tvViewProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
Resource IDs and names are written in lowerCamelCase.
IDs should be prefixed with a shortening of element type in lowercase. For example:
Element | Prefix |
---|---|
TextView |
tv |
ImageView |
iv |
Button |
b |
ViewGroup |
w |
Image view example:
<ImageView
android:id="@+id/ivProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Use w
for wrappers, a.k.a. ViewGroups. For example:
<LinearLayout
android:id="@+id/wUserData"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Menu items id should be prefixed with menu
. For example:
<menu>
<item
android:id="@+id/menuDone"
android:title="Done" />
</menu>
Style names are written in UpperCamelCase and should be prefixed with project name or project name shortening. Also style names should be divided by .
into logical pieces, for example:
<style name="MPay.Theme.Light"/>
<style name="MPay.Text.Header1"/>
<style name="MPay.Button.Flat"/>
Color names are written in lowercase_underscore. Color code should be UPPERCASE, for example:
<color name="grey_light">#F0F0F0</color>
Use prefixes to designate color groups for text text_
, toolbar toolbar_
, status bar status_
, tabs tab_
. Use buttons prefix btn_
for clickable items and selectors. Before adding new color, check if it doesn't already exist in this color group. Do NOT use colors from a different color group.
For colors with a specified alpha value, append opacity percentage as a two digit number at the end of a color name. E.g.
<color name="btn_red_67">#AAFD3C30</color>.
Determine percentage from hex value here.
As a general rule you should try to group similar attributes together. A good way of ordering the most common attributes is:
- View Id
- Style
- Layout width and layout height
- Other layout attributes, sorted alphabetically
- Remaining attributes, sorted alphabetically
These are attributes which are used when the layout is rendered in the tool, but have no impact on the runtime. Use these attributes to display sample data when editing the layout. This makes layout previews much more informative.
To achieve this use tools namespace:
//Make views visible even if they're not visible initially.
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="visible">
//Set sample text if non english string resource is used.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_user_name"
tools:text="Username"/>
//Set sample text if text field is empty initially.
<EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="John Dao"/>
</LinearLayout>
All components align to an 8dp square baseline grid. Iconography in toolbars align to a 4dp square baseline grid. Dimensions that can be reused must be stored in dimension resources, to keep projects visual design language consistent.
Use common dimension resources spacing_* for view paddings and margins to make layouts consistent and better adapt to different screen configurations.
Margins/paddings |
---|
spacing_xs |
spacing_s |
spacing_m |
spacing_l |
spacing_xl |
Use screen margin dimensions for content container that matches screen size. This will provide more appropriate and distinct layout for large screen configurations with minimal effort when separate layout file is not required.
Dimension | Phone | 7" | 10" |
---|---|---|---|
screen_margin_horizontal |
0dp |
64dp |
80dp |
screen_margin_vertical |
0dp |
40dp |
64dp |
If container view is scrollable, screen_margin_vertical
dimension value should be used as vertical padding instead of margin in conjunction with clipToPadding
attribute value set to false, for example:
...
<RecyclerView
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/screen_margin_horizontal"
android:layout_marginRight="@dimen/screen_margin_horizontal"
android:clipToPadding="false"
android:paddingTop="@dimen/screen_margin_vertical"
android:paddingBottom="@dimen/screen_margin_vertical" />
...
Screen margin dimensions can also be used as padding to achieve same visual result when content container is top level container, for example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/screen_margin_horizontal"
android:paddingRight="@dimen/screen_margin_horizontal"
android:paddingTop="@dimen/screen_margin_vertical"
android:paddingBottom="@dimen/screen_margin_vertical">
...
Test classes should match the name of the class the tests are targeting, followed by Test
. For example, if we create a test class that contains tests for the DatabaseHelper
, we should name it DatabaseHelperTest
.
Test methods are annotated with @Test
and should generally start with the name of the method that is being tested, followed by a precondition and/or expected behaviour.
- Template:
@Test void methodNamePreconditionExpectedBehaviour()
- Example:
@Test void signInWithEmptyEmailFails()
Precondition and/or expected behaviour may not always be required if the test is clear enough without them.
Sometimes a class may contain a large amount of methods that at the same time require several tests for each method. In this case, it's recommended to split up the test class into multiple ones. For example, if the DataManager
contains a lot of methods we may want to divide it into DataManagerSignInTest
, DataManagerLoadUsersTest
, etc. Generally you will be able to see which tests belong together because they have common test fixtures.
Every Espresso test class usually targets an Activity, therefore the name should match the name of the targeted Activity followed by Test
, e.g. SignInActivityTest
.
When using the Espresso API it is a common practice to place chained methods in new lines.
onView(withId(R.id.view))
.perform(scrollTo())
.check(matches(isDisplayed()))