Android Coding Style and Guidelines
Last Updated :
20 Jan, 2023
While you writing code, there are some rules you need to follow to keep code easy to read and clean. If you are not new to coding, you probably already know about coding standards. Coding standards vary from person to person and can be different for different projects. Before starting adding or updating code in someone else's project or open source project, you should first look around to identify the coding style they've used and try to follow the same. Here are some commonly accepted coding standards for android which will help you to write clean code.
Standard Coding Style to Follow
1. Indent your code Properly with tabs or 4 white spaces.
2. Put braces on the same line as the code before them, not on their own line.
Example:
Java
// This is Bad practice
class myClass1
{
int func2()
{
//...
}
}
// This is good practice
class MyClass {
int func() {
if(){
//...
}else{
//...
}
}
}
3. If the condition and the body fit on one line, you may put it all on one line.
Example:
Java
if (condition) {
body();
}
// can be written as
if(condition) body();
// But not like
if(condition)
body(); // Bad Practice
4. Use TODO comments for code that is temporary, a short-term solution, or good enough but not perfect. These comments should include the string “TODO” in all caps, followed by a colon.
Example:
Java
// TODO: Use boolean Flag instead of int constant.
5. Switch case should always have a “default” statement to catch unexpected values.
6. Use standard predefined annotations in code wherever needed.
Example:
Java
@Deprecated
// used to mark component which no longer should be used.
@Override
// used to tell that declared method overrides method in superclass.
@Nullable
// tells that parameter, return value of method or field can be null.
@NonNull
// tells that parameter, return value of method or field can not be null.
Handling Exceptions
- You should never leave your catch block empty, even if you are too sure that exception will never occur.
- Try to catch exceptions separately, do not catch generic exceptions.
- Try to use different “try” blocks for statements that can cause different exceptions. Ex.- separate parsing statements from Input statements into separate “try” blocks.
- If you are confident about ignoring exceptions, then you should put an appropriate comment explaining why exceptions can be ignored,
Loops
Example: Declare iteration variable in for loop itself.
Java
int i = 0;
// bad practice
for(i= 0; i< 10; i++)
// Good practice
for(int i=0; i<10; i++){
// do your work
}
// good practice
for(Iterator i = c.iterator(); i.hasNext()){
// do your work
}
Standard Naming Rules to Follow
Naming conventions in coding:
- Non-static, Non-public (i.e., private, default, and protected) field names should start with m, Ex.- int mVar.
- Static field names should start with s, Ex.- private static int sVar.
- Public fields should start with a lower case letter, Ex.- public int var.
- Public static final fields or constants should be in all caps with underscores, Ex.- public static final int MY_CONSTANT = 42.
- Class or interface name should always start with a capital letter and the first letter of the changing word should be also in capital Ex.- MyClass, MyInterface, MyCustomViewClass
- The class should have comments at the top which clearly state the purpose of the class creation. Same for functions.
- The function name should always start with a small letter, and the first letter of the changing word should be capitalized. Ex.- public int registeNewUser(),
Naming conventions in XML:
- Layout files should match the name of the Android components that they are intended. Ex.- layout file for LoginActivity.class should be named as activity_login.xml.
- When we are creating a layout that is going to be inflated by an Adapter, i.e., to populate a ListView or RecyclerView. In this case, the name of the layout should start with “item_”, Ex.- item_contact_card.xml.
- Layout resource id should use the naming convention where possible: <layout name>_<object type>_<object name>, Ex.- profile_imageview_avatar.
- When defining string in strings.xml, name of string should follow this syntax: <purpose>_<where>, Ex.- <string name="hint_login_id">Enter your login ID</string>.
- Drawable files should be named as <prefix>_<description>, prefixes used for icons, buttons, and action Bar are ic_, btn_, ab_ respectively. Ex.- btn_sign_up.xml, dialog_warning.xml, menu_primary.xml.
Handling Resources
All resources that are being used in the project should be defined in the "res" folder of the application in the following format:
- If you need to use string values in the application, then it should be defined in “res/values/string.xml”.
- If you need to use color in the application, then it should be defined in “res/values/colors.xml”.
- If you need to use static arrays in the application, then it should be defined in “res/values/arrays.xml”.
- If you need to use static dimensions in the application, then they should be defined in “res/values/dimens.xml”.
- If you need to use specific or custom styles for components used in the application, it should be defined in “res/values/styles.xml”.
Similar Reads
Lint and its Usage in Android Studio As Android Developers, we all utilize Android Studio to create our apps. There are numerous alternative editors that can be used for Android app development, but what draws us to Android Studio is the assistance that it offers to Android developers. The assistance may take the shape of auto-suggesti
6 min read
What are Design Guidelines? Design guidelines are a set of rules and best practices that help designers create consistent and user-friendly products. They serve as a roadmap, ensuring that all elements of a design work well together and provide a great user experience. Whether youâre designing a website, an app, or any other d
8 min read
Android Applications and Their Categories Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
5 min read
Android | Android Application File Structure It is very important to know about the basics of Android Studio's file structure. In this article, some important files/folders, and their significance is explained for the easy understanding of the Android studio work environment. In the below image, several important files are marked: All of the f
4 min read
Master Android Development With Kotlin: A Complete Guide We regret to inform you that the Android App Development with Kotlin â Live Course by GeeksforGeeks is currently unavailable. For information on related courses and opportunities, please click here.Thank you for your interest.
1 min read
Text Styles in Android TextView displays the declared text in a standard way unless the developer specifies particular attributes to make the text appear better. These attributes can directly be declared into the TextView tags. However, in order to maintain a scalable and clean code, industrial practices suggest gathering
3 min read
How to Build a Simple Notes App in Android? Notes app is used for making short text notes, updating when you need them, and trashing when you are done. It can be used for various functions as you can add your to-do list in this app, some important notes for future reference, etc. The app is very useful in some cases like when you want quick a
9 min read
Android App UI Designing using Sketch and Zeplin Android app UI designing is an essential aspect of app development. A well-designed app can attract users, increase engagement, and improve the overall user experience. However, designing a UI for an Android app can be a challenging task, especially with the constant evolution of Android design guid
4 min read
8 Must-Have Skills for Becoming an Android App Developer Today, Android continues to dominate on a global scale. Nearly 75% of the world population prefers using Android as against the 15% of iOS. It is an operating system that has a huge market for apps. The Google Play Store is home to a staggering 2.87 million apps with each app unique and intuitive in
6 min read
A Complete Guide to Learn XML For Android App Development XML stands for Extensible Markup Language. XML is a markup language much like HTML used to describe data. It is derived from Standard Generalized Markup Language(SGML). Basically, the XML tags are not predefined in XML. We need to implement and define the tags in XML. XML tags define the data and us
7 min read