Skip to content

Android Technical Descriptions (Android code style and naming)

AndreyGrowl edited this page Nov 8, 2017 · 1 revision

Formatting

  • Use standard android studio formatter. Don't add extra new lines except cases when code must be divided into logical blocks.

  • Line-wrapping Break after operators:

#!java
int longVariable = veryLongNamedVariable + anotherLongNamedVariable +
                   fullyNamedComponent + dummyFactory;

//or when passing many parameters
void loadSomething(Context context,
                   String url,
                   ...
                   OnLoadCallback callback);
               

When multiple methods are chained - every .() call should go in separate line:

#!java

            Picasso.with(holder.itemView.getContext())
                    .load(queue.getContentAttributes().getPicture())
                    .into(holder.image);
  • Declare your fields and methods in this order:
    1. Constants

    2. Fields

    3. Constructors

    4. Override methods and callbacks (public or private)

    5. Public methods

    6. Private methods

    7. Inner classes, interfaces or onClickEvents

#!java

public class Example{
    //Fields and variables
    TextView textViewTitle;
    AccountsAdapter adapter;

   //Constructor
    public Example(){

    }

   //LifecycleEvents and overrided methods
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return view;
    }

   //custom methods
    public void loadDummyData(){
    }

   //interface callbacks and onClickEvents
    @OnClick({R.id.btn_cancel, R.id.btn_dismiss})
    public void onClickCancel(){
    }

   //inner classes or interfaces
    static class AnInnerClass {

    }
}
  • Parameter ordering in methods

It's good practice to define parameter start with Context and end with interface callbacks:

#!java
public void loadUserAsync(Context context, int userId, UserCallback callback);

Naming

  • 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: WalletsActivity, ProfileFragment, StealBitcoinService, SharePasswordDialog. Write only general purpose of given compontent.

  • Don't use abbreviation but don't extract the full purpose.

Good:

#!java

SocialAccountsRecyclerViewAdapter
SelectInfoDialogFragment
DatePicker

Bad:

#!java

AccountsAdapter
SelectAdditionalInfoItemDialogFragment
DMYPicker
  • Don't use abbreviations for naming variables but keep only general info.

Good:

#!java

WindowManager.LayoutParams layoutParams;
TextView textViewName;

Bad:

#!java

TextView tvProfile;
ImageView ivImage;
WindoiwManager.LayoutParams wmlp; //true indian style

Name view's resId starting with it's class first word and it's purpose -> input_name, text_message etc.

#!xml

<TextView
   android:id="@+id/text_title" />

<ImageView
   android:id="@+id/image_profile" />
#!java

static class ProfileViewHolder extends RecyclerView.ViewHolder {

    TextView title;
    ImageView image;
}

Bad:

#!xml

<RecyclerView
    android:id="@+id/recycler_view_main_screen /> //in case the recyclerview is only list in this screen

*String constants Declare constants for key-value pair SharedPreferences, Arguments(Bundle) or Intent as static final and use prefix as shown:

  1. SharedPreferences - PREF_
  2. Fragment Arguments- ARG_
  3. Intent Extra - EXTRA_
  4. Intent Action - ACTION_

Resource files

  • Resource file name are written in lowercase_underscore.

LayoutFiles

  • Layout files should start with the Android component it intended. For example for WalletsActivity the name of the layout should be activity_wallets.xml. For lists and cards should be item_wallet.xml and card_profile.xml.

Values files

  • Resource files inside values directory must be plural - strings.xml, dimens.xml, arrays.xml. Arrays must be declared in specific resource file (arrays.xml), not in strings.xml

Android specific

  • Always add ?attrs/selectableItemBackground or ?attrs/selectableItemBackgroundBorderless to clickable views with transparent background if selector isn't specified by design. User should know that he clicked this view.

  • Use toos: for displaying dummy data in layouts files:

#!xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="?attr/selectableItemBackground">

    <ImageView
        android:id="@+id/image_profile"
        tools:src="@drawable/ic_label_active" />

    <TextView
        android:id="@+id/text_name"
        tools:text="Publithor" />
</LinearLayout>

  • Always use .class.getSimpleName(); as tag for logging. Don't write hardcoded tags.

  • Extract the same fields of similar view into styles using built-in tools. Right click on element name

  • then Refactor -> Extract -> Style -> SomeTextViewStyle.

Clone this wiki locally