Skip to content
beginner Phase 2 · Android Fundamentals

Resources

Work with strings, drawables, colors, dimensions, and layout resources. Understand resource qualifiers.

35m
0 problems
Topic Progress 0%

Resource Types

Android Resources

Android resources are non-code assets (strings, images, layouts, dimensions) that your app accesses through generated R class references. Resources are separated from code to support multiple configurations without code changes.

String Resources

<!-- res/values/strings.xml -->
<resources>
    <string name="app_name">My App</string>
    <string name="welcome_message">Hello, %1$s!</string>
    <string name="item_count">%d items</string>
    <string name="price_format">$%.2f</string>
</resources>
// Accessing strings
val name = getString(R.string.app_name)
val welcome = getString(R.string.welcome_message, "Developer")
val count = resources.getQuantityString(R.plurals.item_count, 5, 5)

// In XML
// android:text="@string/app_name"

Plurals

<plurals name="item_count">
    <item quantity="one">%d item</item>
    <item quantity="other">%d items</item>
</plurals>
resources.getQuantityString(R.plurals.item_count, itemCount, itemCount)

Dimension Resources

<!-- res/values/dimens.xml -->
<resources>
    <dimen name="padding_small">8dp</dimen>
    <dimen name="padding_medium">16dp</dimen>
    <dimen name="padding_large">24dp</dimen>
    <dimen name="text_size_title">18sp</dimen>
    <dimen name="corner_radius">12dp</dimen>
</resources>

Use dp for dimensions (density-independent pixels) and sp for text sizes (scaled pixels that respect user font size preferences).

Color Resources

<!-- res/values/colors.xml -->
<resources>
    <color name="primary">#FF1976D2</color>
    <color name="primary_variant">#FF1565C0</color>
    <color name="on_primary">#FFFFFFFF</color>
    <color name="surface">#FFFAFAFA</color>
</resources>

Drawable Resources

Vector drawables are resolution-independent and preferred for icons:

<!-- res/drawable/ic_arrow_back.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <path
        android:fillColor="#FF000000"
        android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z" />
</vector>

Shape drawables for simple backgrounds:

<!-- res/drawable/bg_rounded_button.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/primary" />
    <corners android:radius="8dp" />
    <padding
        android:left="16dp"
        android:top="12dp"
        android:right="16dp"
        android:bottom="12dp" />
</shape>

Layout Resources

<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/padding_medium">

    <TextView
        android:id="@+id/textTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="@dimen/text_size_title"
        android:textStyle="bold" />
</LinearLayout>

Access in code:

// ViewBinding (preferred)
binding.textTitle.text = getString(R.string.app_name)

// findViewById (legacy)
findViewById<TextView>(R.id.textTitle).text = getString(R.string.app_name)

Resource Qualifiers

Resource Qualifiers

Qualifiers let you provide alternative resources for different device configurations. The system selects the best match at runtime.

Common Qualifiers

Qualifier Example Description
values-night values-night/colors.xml Dark theme
values-sw600dp values-sw600dp/dimens.xml Tablets (7")
values-sw720dp values-sw720dp/dimens.xml Large tablets (10")
layout-land layout-land/activity_main.xml Landscape orientation
drawable-hdpi drawable-hdpi/icon.png High density (240dpi)
drawable-xxhdpi drawable-xxhdpi/icon.png Extra-high density (480dpi)
values-fr values-fr/strings.xml French locale
values-night values-night/styles.xml Dark theme styles

Dark Theme Example

<!-- res/values/colors.xml -->
<resources>
    <color name="background">#FFFFFFFF</color>
    <color name="surface">#FFF5F5F5</color>
    <color name="on_surface">#FF1C1B1F</color>
</resources>

<!-- res/values-night/colors.xml -->
<resources>
    <color name="background">#FF1C1B1F</color>
    <color name="surface">#FF2B2930</color>
    <color name="on_surface">#FFE6E1E5</color>
</resources>

Reference the same @color/background in your theme — Android picks the correct color based on system theme.

Screen Size Qualifiers

// Detect screen size programmatically
val isTablet = resources.getBoolean(R.bool.is_tablet)
val width = resources.displayMetrics.widthPixels

// Or using smallest width
val sw = resources.configuration.smallestScreenWidthDp
val isTablet = sw >= 600

Density Buckets

Android devices have different screen densities. Provide images at multiple densities:

Density DPI Scale Factor
mdpi 160 1x (baseline)
hdpi 240 1.5x
xhdpi 320 2x
xxhdpi 480 3x
xxxhdpi 640 4x

A 48x48dp icon needs to be:

  • mdpi: 48x48px
  • hdpi: 72x72px
  • xhdpi: 96x96px
  • xxhdpi: 144x144px
  • xxxhdpi: 192x192px

Prefer vector drawables over PNGs — they scale to any density without quality loss.

Night Mode Configuration

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Force dark theme
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

        // Or follow system setting
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
    }
}

Configuration Change Resources

The system automatically selects resources based on:

  1. Language (locale)
  2. Screen orientation
  3. Screen size (smallest width)
  4. Screen density
  5. Night mode
  6. Color scheme (Android 12+)

You never write conditional code for these — just provide alternative resource directories.

Quiz

1. Why should you use dp for dimensions and sp for text sizes?

Question 1 options

2. What is the primary advantage of vector drawables over PNG images?

Question 2 options

3. What does the values-night resource qualifier do?

Question 3 options

4. How does Android select the best-matching resource?

Question 4 options

Flashcards

Question

What is the difference between dp and sp?

Answer

dp = density-independent pixels (consistent physical size). sp = scaled pixels (also respects user font size preference). Use dp for layouts, sp for text.

Question

What is a vector drawable?

Answer

An XML-based drawable that defines shapes using mathematical paths. Resolution-independent, scales to any density without quality loss.

Question

What is the R class?

Answer

Auto-generated class containing integer constants for every resource in your app. Referenced as R.string.app_name, R.drawable.icon, etc.

Question

How do you provide dark theme colors?

Answer

Create values-night/colors.xml with dark theme color values. The system automatically selects these when dark mode is enabled.

Revision Notes

Key Takeaways

  • 1. Resources separate non-code assets from code for multi-configuration support
  • 2. Use dp for dimensions and sp for text sizes to ensure consistent physical appearance
  • 3. Vector drawables are preferred over PNGs — they scale to any density
  • 4. Resource qualifiers provide automatic configuration-based resource selection
  • 5. ViewBinding provides type-safe resource access without string-based R references

Interview Tips

  • Know the difference between dp, sp, and px and when to use each
  • Explain how resource qualifiers work and give examples
  • Be ready to discuss why vector drawables are preferred over multi-density PNGs
  • Discuss how dark theme is implemented using resource qualifiers

Cheat Sheet

Android Resources Cheat Sheet

Resource Types:

  • values/strings.xml — text
  • values/dimens.xml — dimensions
  • values/colors.xml — colors
  • drawable/ — vector drawables, shapes
  • layout/ — XML layouts
  • mipmap/ — app icons

Key Qualifiers:

  • values-night — dark theme
  • values-sw600dp — tablet
  • layout-land — landscape
  • drawable-hdpi — screen density

Units:

  • dp — density-independent pixels (layouts)
  • sp — scaled pixels (text, respects user preferences)
  • px — physical pixels (avoid)

Access Resources:

  • Code: getString(R.string.name)
  • XML: @string/name
  • ViewBinding: binding.text.text