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:
- Device configuration
- Best match rule
- 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
