- All standards in this document must be followed for
.java
files, while writing code for android apps
- The android log tag helps to easily log statements to the android console for easy debugging of code
- The log tag code must be written immediately after the class opening bracket as shown below
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
}
- Just type
logt
and enter and this line will be automatically typed for you by Android Studio
Standards for variables
- There are three main types for declaring variables
- UI Components
- Variables
- Constants
- All variable identifier must be declared as
private
except for constants and static variables - The names of all UI components must be in
CamelCase
and start withm
, for example
private TextView mUsername;
private Button mSubmit;
private RecyclerView mRecyclerView;
- The names of all constants must be written in
CAPS
and seperated with_
, for example
public static final int CAMERA_REQUEST_CODE = 440;
public static final String USER_KEY = "dan";
- The names of all variables must also be in
CamelCase
and may or may not start withm
, for example
both examples 1 and 2 are correct here:
1) private ArrayList<String> mUsernames = new ArrayList<>();
2) private ArrayList<String> allUserBooksList = new ArrayList<>();
- Variables must be declared exactly after the
TAG
line - While declaring any variable the corresponding comment must be used:
- For UI components:
//ui components
- For Variables:
//vars
- For Constants:
//constants
- For UI components:
- As shown below:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
//constants
private static final int PERMISSION_CODE = 12;
private static final String FALSE_API_KEY = "uRIgTh";
//ui components
private TextView mUsername, mPassword, mBirthDate;
private Button mRegister, mLogin;
private ImageView mProfilePhoto;
//vars
private ArrayList<String> mUsernames = new ArrayList<>();
private ArrayList<String> mBirthDates = new ArrayList<>();
private Context mContext;
}
- All variables and UI Components must be initialized first inside
onCreate
- For Example:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsername = findViewById(R.id.mUsername);
mPassword = findViewById(R.id.mPassword);
mProfilePhoto = findViewById(R.id.mProfilePhoto);
mContext = getApplicationContext();
.
.
.
}
"The more useful methods you write the better coder you are" - Mitch Tabian, CodingWithMitch
- All code that is to be used more than once must be written in a method
- Method names must be chosen very wisely
- Opening bracket of the method must be on the same line as the method name, for example
//WRONG WAY
private String getName()
{
//statements
}
//CORRECT WAY
private String getName() {
//statements
}
- Method names must be strictly written in
CamelCase
, for example
private void addUserToDatabase() {
}
- A method name must describe clearly what the method will do, for example, a method that sends data to firebase
//WRONG METHOD NAMING
private void sendData() {
}
//CORRECT METHOD NAMING
private void sendDataToFirebase() {
}
Log.d(TAG, "This is a log statement");
- For easy debugging, all methods must be logged
- Inside any method, the first statement must be a log statement, as shown below
// EXAMPLE 1
public void setupRecyclerView(){
Log.d(TAG, "setupRecyclerView: setting up recyclerview");
.
.
.
}
//EXAMPLE 2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started");
.
.
.
}
- In android, we use the
Log.d()
method to log a statement, for example - Just type
logt
and Android Studio will automatically log a statement for you
- All
.java
code files must be in their appropriate packages, for example- All Activities in
activities
- All Fragments in
fragments
- All Model classes in
models
- All RecyclerView Adapters in
adapters
- All Utility files in
util
, etc...
- All Activities in
- Below is an example,
- All activity file names must start with a capital letter and end with Activity.java
For example: MainActivity.java, DisplaySongActivity.java, etc...
- All fragment file names must start with a capital letter and end with Fragment.java
For example: ChooseFruitFragment.java, ShowColorsFragment.java, etc...
- All Interface names must begin with a capital
I
and then the interface name must begin with a capital letter
For example: ISearchable, IMainInterface, IParcelable, etc...
- Any other
.java
file too must begin with capital letters
For example: BookRecyclerAdapter.java, SharedPrefHelper.java, etc...