Naming Convention for Android Project

There are actually no official documents released about naming conventions in Android projects. So, some of you might think that there is no need to have naming conventions in our Android projects. But with a defined naming convention it increases readability and maintainability of code base. Here I am sharing naming convention that I use in a android project developed in Java in Android Studio.

1 File naming

1.1.1 Class files

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.

1.2.1 Resources files

Resources file names are written in lowercase_underscore.

1.2.2 Drawable files

Naming conventions for drawables:

Naming conventions for icons (taken from Android iconography guidelines):

Naming conventions for selector states:

1.2.3 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. For example, if we are creating a layout for the SignInActivity, the name of the layout file should be activity_sign_in.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_.
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_.

1.2.4 Menu files

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.

1.2.5 Values files

Resource files in the values folder should be plural, e.g. strings.xml, styles.xml, colors.xml, dimens.xml, attrs.xml


2. Java

2.1.1 Fields definition and naming

Fields should be defined at the top of the file and they should follow the naming rules listed below.
  • 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.

Example:

public class MyClass {
    public static final int SOME_CONSTANT = 42;
    public int publicField;
    private static MyClass sSingleton;
    int mPackagePrivate;
    private int mPrivate;
    protected int mProtected; 
}

2.2.1 Treat acronyms as words


2.3.1 String constants, naming, and values

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.

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.
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 ARGUMENT_USER_ID = "ARGUMENT_USER_ID";

// Intent-related items use full package name as value
static final String EXTRA_SURNAME = "com.myapp.extras.EXTRA_SURNAME";
static final String ACTION_OPEN_USER = "com.myapp.action.ACTION_OPEN_USER";
}

3. XML

3.1.1 Resources naming

Resource IDs and names are written in lowercase_underscore.

3.2.1 ID naming

IDs should be prefixed with the name of the element in lowercase underscore. For example:




3.3.1 Strings

String names start with a prefix that identifies the section they belong to. For example registration_email_hint or registration_name_hint. If a string doesn't belong to any section, then you should follow the rules below:

3.4.1 Styles and Themes

Unlike the rest of resources, style names are written in UpperCamelCase.

Comments