Resources in Android

Excellent theory question ๐Ÿ‘ (Very common in Android exams)

Alternate resources in Android allow the app to automatically use different resources based on device configuration like screen size, language, orientation, or API level.

Android automatically selects the best matching resource folder at runtime.


๐Ÿ“Œ What Are Alternate Resources?

Alternate resources are different versions of the same resource placed in specially named folders inside the res/ directory.

Android chooses the correct folder depending on:

  • Screen size
  • Orientation
  • Language
  • Screen density
  • API level
  • Night mode
  • etc.

๐Ÿ“‚ General Folder Format

res/
   resource_name-qualifier/

Example:

res/layout-land/
res/values-fr/
res/drawable-hdpi/

๐Ÿ”น 1๏ธโƒฃ Using Alternate Layout for Orientation

๐Ÿ“Œ Example: Portrait & Landscape Layout

Folder Structure

res/layout/activity_main.xml
res/layout-land/activity_main.xml
  • layout/ โ†’ Portrait (default)
  • layout-land/ โ†’ Landscape

Example

res/layout/activity_main.xml

<TextView
    android:text="Portrait Mode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

res/layout-land/activity_main.xml

<TextView
    android:text="Landscape Mode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

๐Ÿ“Œ When device rotates โ†’ Android automatically loads layout-land.


๐Ÿ”น 2๏ธโƒฃ Using Alternate Resources for Language (Localization)

๐Ÿ“Œ Example: English & Hindi

Folder Structure

res/values/strings.xml
res/values-hi/strings.xml

Default English

res/values/strings.xml

<string name="welcome">Welcome</string>

Hindi Version

res/values-hi/strings.xml

<string name="welcome">เคธเฅเคตเคพเค—เคค เคนเฅˆ</string>

๐Ÿ“Œ If phone language = Hindi โ†’ Android loads values-hi.


๐Ÿ”น 3๏ธโƒฃ Using Alternate Drawables for Screen Density

Different images for different screen resolutions.

Folder Structure

res/drawable-mdpi/
res/drawable-hdpi/
res/drawable-xhdpi/

Example:

res/drawable-mdpi/logo.png
res/drawable-hdpi/logo.png

๐Ÿ“Œ Android automatically selects best image quality.


๐Ÿ”น 4๏ธโƒฃ Using Alternate Resources for Screen Size

Folder Structure

res/layout/
res/layout-sw600dp/
  • sw600dp โ†’ tablets

Example:

layout/activity_main.xml โ†’ Phone layout
layout-sw600dp/activity_main.xml โ†’ Tablet layout

๐Ÿ“Œ Used for responsive UI design.


๐Ÿ”น 5๏ธโƒฃ Using Alternate Resources for API Version

Example:

res/values/
res/values-v21/
  • v21 โ†’ Android 5.0+

Example:

res/values/styles.xml
res/values-v21/styles.xml

Used when certain features only supported in newer Android versions.


๐Ÿ”น 6๏ธโƒฃ Night Mode Resources

res/values-night/

Example:

res/values/colors.xml

<color name="background">#FFFFFF</color>

res/values-night/colors.xml

<color name="background">#000000</color>

๐Ÿ“Œ Automatically changes when dark mode enabled.


๐Ÿ”น 7๏ธโƒฃ Smallest Width Qualifier

Example:

layout-sw320dp
layout-sw600dp
layout-sw720dp

Used for advanced responsive layouts.


๐Ÿ“Œ How Android Chooses Resources

Android uses:

  1. Device configuration
  2. Best match rule
  3. Default fallback if no match

Example:
If no layout-land โ†’ Android uses layout.


๐ŸŽฏ Advantages

โœ… Automatic device adaptation
โœ… No manual coding required
โœ… Better UI/UX
โœ… Supports multi-language
โœ… Optimized performance


โŒ Disadvantages

โŒ More folders to manage
โŒ Large project structure
โŒ Confusing for beginners