Skip to content

Latest commit

 

History

History
163 lines (125 loc) · 6.56 KB

ResourceStyleGuide.md

File metadata and controls

163 lines (125 loc) · 6.56 KB

Resource Style Guidelines for Android

New projects should follow the Android Gradle project structure that is defined on the Android Gradle plugin user guide.

Resource Files

Resources file names are written in lowercase_underscore.

Drawable Files

Naming conventions for drawables:

Asset Type Prefix Example
Action bar action_ action_stacked.9.png
Button button_ button_send_pressed.9.png
Dialog dialog_ dialog_top.9.png
Divider divider_ divider_horizontal.9.png
Icon ic_ ic_star.png
Menu menu_ menu_submenu_bg.9.png
Notification notification_ notification_bg.9.png
Tabs tab_ tab_pressed.9.png

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

Asset Type Prefix Example
Icons ic_ ic_star.png
Launcher icons ic_launcher ic_launcher_calendar.png
Menu and Action Bar icons ic_menu ic_menu_archive.png
Status bar icons ic_notify ic_notify_msg.png
Tab icons ic_tab ic_tab_recent.png
Dialog icons ic_dialog ic_dialog_info.png

Naming conventions for selector states:

State Suffix Example
Normal _normal btn_order_normal.9.png
Pressed _pressed btn_order_pressed.9.png
Focused _focused btn_order_focused.9.png
Disabled _disabled btn_order_disabled.9.png
Selected _selected btn_order_selected.9.png

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.

Component Class Name Layout Name
Activity UserProfileActivity activity_user_profile.xml
Fragment SignUpFragment fragment_sign_up.xml
Dialog ChangePasswordDialog dialog_change_password.xml
Adapter item --- item_person.xml
Partial layout --- partial_stats_bar.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 RecyclerView. 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_.

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.

Values Files

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

Resource Naming

Resource IDs and names are written in lowercase_underscore.

IDs

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

Element Prefix
TextView text_
ImageView image_
Button button_
Menu menu_

Image view example:

<ImageView
    android:id="@+id/image_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Menu example:

<menu>
	<item
        android:id="@+id/menu_done"
        android:title="@string/menu_done" />
</menu>

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:

Prefix Description
error_ An error message
msg_ A regular information message
title_ A title, i.e. a dialog title
action_ An action such as “Save” or “Create”
menu_ A menu item such as “Done”

Styles and Themes

Style names are written in UpperCamelCase.

XML Style Rules

Use Self Closing Tags

When an XML element doesn’t have any contents, you must use self closing tags.

This is good:

<TextView
    android:id="@+id/text_view_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

This is bad:

<!-- Don’t do this! -->
<TextView
    android:id="@+id/text_view_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</TextView>

Attribute Ordering

As a general rule you should try to group similar attributes together. A good way of ordering the most common attributes is:

  1. View Id
  2. Layout width and height
  3. Other layout attributes, sorted alphabetically
  4. Remaining attributes, sorted alphabetically
  5. Style
  6. Tools attributes, sorted alphabetically

Each attribute should also be written on a new line.

This is good:

<TextView
    android:id="@+id/text_view_profile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

This is bad:

<TextView android:id="@+id/text_view_profile"
    android:layout_width="wrap_content" android:layout_height="wrap_content" />