In an Android project, the res/
(resources) folder contains all the non-code assets like UI layouts, images, themes, colors, and strings. These resources help in creating a scalable, multilingual, and responsive UI.
π res/ Directory Overview
app/src/main/res/
βββ drawable/
βββ mipmap/
βββ layout/
βββ values/
βββ raw/
βββ xml/
βββ font/
βββ animator/
βββ anim/
βββ menu/
βββ color/
Each folder has a specific purpose. Letβs discuss them in detail.
πΉ 1. drawable/
(Images & Graphics) π¨
Contains images, shape drawables, and vector assets used in UI elements.
π Types of Drawables:
File Type | Description | Example File |
---|---|---|
Bitmap Images | Stores PNG, JPG, WEBP images | logo.png , background.jpg |
Vector Drawables | XML-based scalable images | ic_launcher.xml |
Shape Drawables | XML files for gradients, borders, buttons | rounded_button.xml |
State Drawables | XML for button states (pressed, focused) | button_selector.xml |
π Example: Button Shape Drawable (button_background.xml
)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FF6200EE"/>
<corners android:radius="8dp"/>
</shape>
πΉ Best Practice: Use Vector Drawables instead of PNGs to save storage.
πΉ 2. mipmap/
(App Icons) ποΈ
- Stores app launcher icons in different resolutions.
- Used instead of
drawable/
for better scalability and performance. - Common sizes:CopyEdit
mipmap-hdpi/ mipmap-mdpi/ mipmap-xhdpi/ mipmap-xxhdpi/ mipmap-xxxhdpi/
π Example: Setting App Icon in AndroidManifest.xml
<application
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round">
</application>
πΉ Best Practice: Use adaptive icons for consistency across devices.
πΉ 3. layout/
(UI Layout Files) π
- Contains XML files that define the user interface (UI) structure.
- Used for activities, fragments, RecyclerView items, dialogs, etc.
π Example File Structure:
res/layout/
βββ activity_main.xml
βββ fragment_dashboard.xml
βββ item_list.xml
π Example: Activity Layout (activity_main.xml
)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textTitle"
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
πΉ Best Practice: Use ConstraintLayout for flexible and responsive UI design.
πΉ 4. values/
(App-wide Constants) ποΈ
Contains XML files storing reusable values like colors, dimensions, styles, and strings.
π Common Files in values/
Directory:
File | Purpose |
---|---|
strings.xml | Stores text for localization. |
colors.xml | Defines app color palette. |
dimens.xml | Stores dimensions (margins, text sizes). |
styles.xml | Stores themes & styles for UI consistency. |
π Example: strings.xml
<resources>
<string name="app_name">MyApp</string>
<string name="welcome_message">Welcome to MyApp!</string>
</resources>
π Example: colors.xml
<resources>
<color name="primaryColor">#6200EE</color>
<color name="background">#FFFFFF</color>
</resources>
πΉ Best Practice: Never hardcode text and colors in XML or Java/Kotlin. Use values/
instead.
πΉ 5. raw/
(Media & Other Files) π΅
- Stores raw assets like MP3, MP4, JSON, text files.
- Used for audio, video, configuration files, etc.
π Example: Play Audio from raw/
Folder
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sound_file);
mediaPlayer.start();
πΉ Best Practice: Use res/raw/
for media files instead of assets/
.
πΉ 6. xml/
(App Configurations) βοΈ
- Contains configuration files like permissions, preferences, and network security settings.
π Common Files:
File | Purpose |
---|---|
network_security_config.xml | Defines security settings (HTTPS rules). |
preferences.xml | Stores shared preferences (settings). |
π Example: network_security_config.xml
<network-security-config>
<domain-config cleartextTrafficPermitted="false">
<domain includeSubdomains="true">example.com</domain>
</domain-config>
</network-security-config>
πΉ Best Practice: Use xml/
for app-wide settings instead of hardcoding.
πΉ 7. font/
(Custom Fonts) π
- Stores custom fonts for app text styling.
- Supports TTF and OTF font files.
π Example: Adding Custom Font (font/roboto.ttf
)
<TextView
android:text="Hello"
android:fontFamily="@font/roboto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
πΉ Best Practice: Store all fonts in res/font/
for easy access.
πΉ 8. anim/
& animator/
(Animations) π¬
- Stores animation XML files for UI effects.
π Example: Fade In Animation (res/anim/fade_in.xml
)
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
πΉ Best Practice: Use animator/
for property animations and anim/
for view animations.
πΉ 9. menu/
(Navigation Menus) π
- Defines toolbar menus, bottom navigation, and contextual menus.
π Example: Toolbar Menu (menu_main.xml
)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_settings"
android:title="Settings"
android:icon="@drawable/ic_settings"
android:showAsAction="always"/>
</menu>
πΉ Best Practice: Use menu/
for toolbar and bottom navigation menus.
π Summary: Why Use Default Resource Folders?
β
Better organization of assets.
β
Easier localization & theme management.
β
Efficient memory & performance optimization.
β
Improves code maintainability and scalability.