Resources: Strings, Colors & Dimensions
Resources: Strings, Colors & Dimensions
One of Android's most powerful architectural decisions is the complete separation of values from code. Colors, text, sizes, images — none of these belong hardcoded inside a Java class. They live in XML resource files under res/, and the build system generates a class called R that gives your code type-safe integer handles to each one. This lesson covers the three most important value resource types — strings, colors, and dimensions — and explains how to use them correctly at every level of the stack.
Why Externalize Values?
Consider what happens when you hardcode a color directly in Java:
Now imagine the designer changes the brand color. You must grep through every Java file, find every hex literal, and hope you caught them all. If you later add Arabic support, every hardcoded string becomes a maintenance burden.
With resource files you change one line in colors.xml and every widget that references @color/brand_primary updates automatically — in all layouts, all themes, all screen densities and all languages.
The res/ Directory Structure
Android organizes resources into typed subdirectories under app/src/main/res/:
res/values/strings.xml— string constants and pluralsres/values/colors.xml— color constantsres/values/dimens.xml— dimension constants (dp, sp)res/values/styles.xml— styles and themesres/drawable/— vector and raster graphicsres/layout/— XML layout filesres/values-ar/strings.xml— Arabic string overrides (localization)res/values-night/colors.xml— dark-mode color overrides
-ar, -night, -sw600dp. Android's resource resolver picks the best-matching folder at runtime. Your default values/ folder is the fallback when no qualifier matches. This single mechanism handles localization, dark mode, screen size, density, and orientation — all without a single if statement in Java.
Strings
Every user-visible string should live in res/values/strings.xml:
Reference a string inside a layout XML with the @string/ prefix:
Load a string in Java code with getString() — available on any Context (Activity, Fragment, Service):
getString(R.string.items_count, cart.size()) lets translators reorder the sentence naturally in their language. With concatenation you force every language into the English word order, which often reads badly in Arabic, Japanese, or German.
Plurals
Many languages have more than two plural forms. Use <plurals> to handle them correctly:
Colors
Colors belong in res/values/colors.xml as ARGB or RGB hex literals:
Use colors in layout XML with @color/:
Resolve a color in Java with ContextCompat.getColor():
getResources().getColor(int) directly. It was deprecated in API 23 because it ignores the current theme. Always use ContextCompat.getColor(context, R.color.xxx), which is safe on all API levels and respects themes correctly.
Dimensions
Dimensions are stored in res/values/dimens.xml. Android uses two units you must know:
- dp (density-independent pixel) — use for all layout sizes (margins, padding, widths, heights). 1 dp = 1 physical pixel on a 160 dpi screen; the system scales automatically on higher-density displays.
- sp (scale-independent pixel) — use exclusively for text sizes. Like dp but also scales with the user's system font-size preference. Never set
textSizein dp.
Apply dimensions in XML:
Read a dimension in Java (returns pixels, already scaled for the current display):
Accessing Resources — The R Class
Every time you build, the Android Gradle plugin generates R.java (or an equivalent binary artifact). It contains nested static classes — R.string, R.color, R.dimen, R.id, R.layout, and so on — each holding integer constants that map to the actual resource. You never construct or mutate this class; you only read from it.
Build > Make Project. The R class is regenerated on every successful build.
Localization: Providing Alternative Strings
To translate strings into Arabic, create res/values-ar/strings.xml and provide only the strings that differ. You do not need to duplicate every string — only override what changes:
When the device locale is Arabic, getString(R.string.login_button) automatically returns "تسجيل الدخول" without any code change whatsoever. This is the entire value proposition of the resource system.
Summary
Resource files are not a formality — they are the mechanism by which your app becomes correct on every device, locale, and theme without branching logic in Java. Keep all user-visible strings in strings.xml, all colors in colors.xml, and all layout measurements in dimens.xml. Reference them with @string/, @color/, @dimen/ in XML and with the R class in Java. In the next lesson you will use Intents to navigate between two Activities — and the labels and colors you define here will travel with you.