Android Resource Formatting Standards
Naming
- XML generated resources (e.g. R.id.some_id) should be named in snake case. Though capitals are allowed, the compiler will reject files named with capitals. For consistency it is important to keep naming conventions the same.
Layouts
- Put the xml declaration on the first line with the root tag on the second line with no indentation.
- Use 4 spaces for indenting tags. Use 4 spaces for parameters.
- Add a blank line between tags except for closing
ViewGroup
tags.
- Put ID as the first parameter.
layout_width
and layout_height
should be at the top, in that order (below ID).
- Favor
ConstraintLayout
and its layout helpers over nesting several layouts
- Keep
layout_
prefixed parameters grouped towards the top with width and height.
- Keep
tools:
parameters grouped at the bottom.
- Try to keep related items near each other (textSize, textColor; shadowColor, shadowRadius, etc..)
- You may include HTML style comments if you like:
<!-- HTML Comments -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/char_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
tools:visibility="visible"
android:src="@drawable/img_char_bottom"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="@+id/skip_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Button.Dialog.Skip"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:text="@string/skip"/>
<Button
android:id="@+id/next_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Button.Dialog.Next"
android:text="@string/next"/>
</LinearLayout>
</RelativeLayout>
Styles
- Use styles whenever possible, they make for cleaner layout files.
- Group related styles by name:
<style name="HorizontalLine">
<item name="android:layout_height">1dip</item>
<item name="android:layout_width">match_parent</item>
</style>
<style name="HorizontalLine.Estimate">
<item name="android:background">#ff8fa2b2</item>
</style>
<style name="EstimateNote">
<item name="android:paddingLeft">36dip</item>
</style>
<style name="EstimateTotal">
<item name="android:textSize">60sp</item>
</style>
- Try to give styles as much context as possible. Do not include color names as part of the style. This makes things difficult to understand or change if the color changes:
Bad:
<style name="Button">
<item name="android:textColor">#ffffffff</item>
</style>
<style name="Button.Green">
<item name="android:background">@drawable/green_button_selector</item>
</style>
<style name="Button.Blue">
<item name="android:background">@drawable/blue_button_selector</item>
</style>
Colors
- Use colors anywhere you need a color value
- Keep common colors from having very little context:
Good:
<!-- Context free color definition -->
<color name="white">#ffffffff</color>
<color name="light_gray">#ffcccccc</color>
<color name="dark_gray">#ff333234</color>
<color name="dscout_teal">#ff02b2e0</color>
Bad:
<color name="ok_button_blue_color">#ff0000ff</color> <!-- This is now green? -->
- When using colors that do require context they must refer to a context free color or an absolute color value:
Good:
<color name="snippet_detail_border_color">@color/white</color>
<color name="snippet_detail_bg_color">@color/light_gray</color>
<color name="snippet_question_title">@color/dscout_teal</color>
<color name="snippet_question_details">@color/dark_gray</color>
Strings
- Always use a strings file when defining copy. Period. No Exceptions.
- Leverage plurals and formatted string definitions when applicable. They provide a cleaner implementation than in code solutions.
Dimensions
- Dimensions can provide a localized approach to common dimensions. Consider them for cases such as cell spacing used multiple places in the app. This allows for quick edits across the whole app.
Additional Information
App Resources
Styles and Themes
String Resources
Dimension Resources