Resource Organization - Vadym-Popovych24/Template GitHub Wiki
This document describes how resources are organized in the Android application template. Understanding resource organization is essential for effective Android development as it provides a structured way to manage various app assets including layouts, strings, drawables, and other resources.
For information about the overall architecture, see Architecture. For details about the build process that compiles these resources, see Build Process.
Android resources are organized in a structured directory hierarchy under the res directory. Each resource type has its own dedicated subdirectory.
Android resources are categorized into different types, each stored in its respective directory:
Resource Type | Directory | Description | Example Files |
Layout | layout/ | XML files that define the user interface | activity_main.xml, fragment_profile.xml |
Drawable | drawable/ | Image files and XML drawable definitions | background.png, button_selector.xml |
Values | values/ | XML files containing simple values | strings.xml, colors.xml, styles.xml |
Mipmap | mipmap/ | Launcher icons at different densities | ic_launcher.png |
Menu | menu/ | XML files defining application menus | main_menu.xml |
Raw | raw/ | Raw files saved in their raw form | video.mp4, audio.mp3 |
XML | xml/ | Arbitrary XML files to be read at runtime | network_security_config.xml |
Animator | animator/ | XML files defining property animations | fade_in.xml |
Animation | anim/ | XML files defining tween animations | slide_in.xml |
Font | font/ | Font files | roboto.ttf |
The template follows standard Android naming conventions for resources to ensure consistency and readability.
Layout naming pattern: type_description.xml
- activity_: Layouts for activities
- fragment_: Layouts for fragments
- item_: Layouts for items in lists/recycler views
- view_: Layouts for custom views
- dialog_: Layouts for dialogs
- layout_: Reusable layouts (to be included)
Drawable naming pattern: type_description.extension
- ic_: Icons
- bg_: Backgrounds
- divider_: Dividers
- btn_: Button graphics
- selector_: State selectors
Android supports different resource configurations based on device characteristics using resource qualifiers. This allows for automatic resource selection based on the device's configuration.
Common resource qualifiers include:
Qualifier Type | Examples | Description |
Screen density | hdpi, xhdpi, xxhdpi | Different screen densities |
Language | en, fr, de | Language localization |
Screen size | small, normal, large, xlarge | Different screen sizes |
Screen orientation | port, land | Portrait or landscape orientation |
API Level | v21, v23 | Specific Android API levels |
Night mode | night | Dark theme resources |
Resources are accessed in code and XML through Android's resource reference system. This diagram illustrates how resources are referenced and used:
Resources are accessed in code through the automatically generated R class:
// In Java/Kotlin code
setContentView(R.layout.activity_main);
textView.setText(R.string.hello_world);
imageView.setImageResource(R.drawable.ic_launcher);
In XML, resources are referenced using the @ notation:
<!-- In XML files -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
Different resource files have specific structures. Here are examples of common resource file formats:
<resources>
<string name="app_name">Template App</string>
<string name="welcome_message">Welcome to the app!</string>
<!-- String arrays -->
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
</string-array>
<!-- Plurals -->
<plurals name="numberOfSongs">
<item quantity="one">1 song found</item>
<item quantity="other">%d songs found</item>
</plurals>
</resources>
<resources>
<color name="primary">#6200EE</color>
<color name="primary_dark">#3700B3</color>
<color name="accent">#03DAC6</color>
<color name="background">#FFFFFF</color>
<color name="text_primary">#212121</color>
</resources>
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="text_size_small">12sp</dimen>
<dimen name="text_size_medium">16sp</dimen>
<dimen name="text_size_large">20sp</dimen>
</resources>
The following diagram illustrates how Android selects the appropriate resources at runtime:
Android follows a specific algorithm to select resources:
- Filter out resources that don't match the current device configuration
- Choose the most specific matching resource configuration
- Follow configuration-specific priority rules for resolution
Resources interact with other application components as shown in this diagram:
This diagram shows how different resource types are connected to the components that use them, bridging the gap between resource files and code entities.
The template follows these resource organization best practices:
- Consistent Naming: Use consistent prefixes and descriptive names for all resources
- Resource Externalization: Keep hardcoded values out of code by storing them in appropriate resource files
- Density Support: Provide images for different screen densities (hdpi, xhdpi, xxhdpi, etc.)
- Localization Ready: Externalize strings for easy translation into other languages
- Theme and Style Use: Use styles and themes for consistent UI appearance across the app
- Layout Optimization: Avoid deeply nested layouts for better performance
- Resource Reuse: Create reusable resources whenever possible to maintain consistency
The Android resource organization system provides a structured way to manage app assets while supporting different device configurations. By following resource naming conventions and organizational best practices, the template ensures that applications are maintainable, localizable, and provide a consistent user experience across different devices.
For information on how to use this template to start your own Android application development, see Getting Started.