Skip to content

Instantly share code, notes, and snippets.

@alex-amenos
Created December 1, 2019 01:19
Show Gist options
  • Save alex-amenos/c1e4481ee0bd29108b654b06eb0a2cb4 to your computer and use it in GitHub Desktop.
Save alex-amenos/c1e4481ee0bd29108b654b06eb0a2cb4 to your computer and use it in GitHub Desktop.
Naming conventions

Modules

All the module resources have a module prefix.

On build.gradle file add the following lines to accomplish this rule with the module name

android {
   resourcePrefix 'module_name'
}

Layout

<MODULE>_<COMPONENT>_<DESCRIPTION>

  • <MODULE>: The module name where you are creating the layout.
  • <COMPONENT>: Activity, Fragment, Dialog, AdapterView Item, View , etc.
  • <DESCRIPTION>: Any extra information.

Examples:

Taking an account we are on user module:

Component Class Name Layout Name
Activity UserProfileActivity user_activity_profile.xml
Fragment UserDetailFragment user_fragment_detail.xml
Dialog UserDialogChangePassword user_dialog_change_password.xml
AdapterView item - user_item_person.xml
Custom view - user_view_custom.xml

Using Kotlin language, you can manage camelcase for IDs (reason: Synthetic Binding).

Some view identifiers examples:

userDetailFragment
userDetailNameTextView
userDetailProfileImageView

Drawables

Taking an account of all the drawable types (vector drawable and no vector drawable).

<MODULE>_<WHAT>_<DESCRIPTION>

  • <MODULE>: The module name you are working on.
  • <WHAT>: Button, Dialog, Divider, Icon, etc .
  • <DESCRIPTION>: Any extra information.

Examples:

Taking an account we are on user module:

Component Prefix Layout Name
Action bar ab_ user_ab_stacked_9.png
Button btn_ user_btn_send_pressed_9.png
Dialog dialog_ user_dialog_top_9.png
Divider divider_ user_divider_horizontal_9.png
Icon ic_ user_ic_star.png
Menu menu_ user_menu_submenu_bg_9.png
Notification notification_ user_notification_bg_9.png

Dimension

<MODULE>_<WHAT>_<SIZE>

  • Dimension files are independent per module (not allowed to share dimensions between modules).
  • <MODULE>: The module name you are working on.
  • <WHAT>: It can be...
    • width (in dp)
    • height (in dp),
    • size (if width == height)
    • margin (in dp)
    • padding (in dp)
    • elevation (in dp)
    • text_size (in sp)
    • ...
  • <SIZE>: It can be on dp or sp.

Examples:

<!-- Margin -->
<dimen name="users_margin_16_dp">16dp</dimen>

<!-- Padding -->
<dimen name="users_padding_16_dp">16dp</dimen>

<!-- Width -->
<dimen name="users_width_50_dp">50dp</dimen>

<!-- Height -->
<dimen name="users_height_50_dp">50dp</dimen>

<!-- if width == height, we use size -->
<dimen name="users_size_10dp">10dp</dimen>

<!-- Text size -->
<dimen name="users_text_size_15sp">15sp</dimen>
<dimen name="users_text_size_20sp">20sp</dimen>

Attributes

All colors, texts appearance, icons and images have to be based on attributes and the attributes to a style module theme.

It is because maybe on future is needed to apply changes and using this way, it should be easier.

<MODULE>_<WHAT>_<DESCRIPTION>

  • <MODULE>: The module name you are working on.
  • <WHAT>: color, text, drawable.
  • <DESCRIPTION>: Any extra information.

Examples:

<?xml version="1.0" encoding="utf-8"?>
<resources>

   <declare-styleable name="UsersTheme">
   
      <!-- Color -->
      <attr name="users_color_divider" format="color" />
      <attr name="users_color_disabled" format="color" />
      <attr name="users_color_text" format="color" />
      
      <!-- Text -->
      <attr name="users_text_header" format="reference" />
      <attr name="users_text_title" format="reference" />
      <attr name="users_text_body" format="reference" />
      <attr name="users_text_description" format="reference" />
      
      <!-- Drawable -->
      <attr name="users_drawable_icon_divider" format="reference" />
      <attr name="users_drawable_profile_image" format="reference" />
      <attr name="users_drawable_profile_placeholder" format="reference" />
      <attr name="users_drawable_profile_cell" format="reference" />
      <attr name="users_drawable_profile_phone" format="reference" />
      <attr name="users_drawable_profile_email" format="reference" />
      <attr name="users_drawable_profile_username" format="reference" />
      <attr name="users_drawable_profile_timezone" format="reference" />
      <attr name="users_drawable_profile_address" format="reference" />
      <attr name="users_drawable_profile_location" format="reference" />
      
   </declare-styleable>

</resources>

Styles

All the module attributes are based on a module style.

The style names are the module name and based on the AppTheme (it is defined on core module).

You should reuse the AppTheme and the core styles (styles of Core module) if it is possible, otherwise, you can create a new module style (using attributes and styles).

Examples:

<resources>

   <!-- Module style -->
   <style name="UsersTheme" parent="AppTheme">
   
      <!-- Colors -->
      <item name="users_color_divider">@color/gray_90</item>
      <item name="users_color_disabled">@color/gray_60</item>
      <item name="users_color_text">@color/carbon</item>
      
      <!-- Text -->
      <item name="users_text_header">@style/TextHeader</item>
      <item name="users_text_title">@style/TextTitle</item>
      <item name="users_text_body">@style/TextBody</item>
      <item name="users_text_description">@style/TextDescription</item>
      
      <!-- Drawable -->
      <item name="users_drawable_icon_divider">@drawable/users_divider_rv_icon</item>
      <item name="users_drawable_profile_image">@drawable/users_bg_profile_image</item>
      <item name="users_drawable_profile_placeholder">@drawable/users_ic_user_placeholder</item>
      <item name="users_drawable_profile_cell">@drawable/users_ic_cell</item>
      <item name="users_drawable_profile_phone">@drawable/users_ic_phone</item>
      <item name="users_drawable_profile_email">@drawable/users_ic_email</item>
      <item name="users_drawable_profile_username">@drawable/users_ic_username</item>
      <item name="users_drawable_profile_timezone">@drawable/users_ic_timezone</item>
      <item name="users_drawable_profile_address">@drawable/users_ic_home</item>
      <item name="users_drawable_profile_location">@drawable/users_ic_location</item>
      
    </style>
   
   <!-- Text style -->
    <style name="TextHeader">
        <item name="android:textColor">@color/green_radioactive</item>
        <item name="android:textSize">22sp</item>
        <item name="fontPath">fonts/roboto_bold.ttf</item>
    </style>
    
    <style name="TextTitle">
        <item name="android:textColor">@color/carbon</item>
        <item name="android:textSize">18sp</item>>
        <item name="fontPath">fonts/roboto_bold.ttf</item>
    </style>

    ...

</resources>

Exception

The Core module is not forcing the rule to add module prefix on all the resources, but you should follow the rule on all the resources, with only one exception:

  • You can avoid the prefix rule on the core string resources.

Help links about naming conventions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment