Load dimension value from res/values/dimension.xml from source code
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Android dimensions defined in res/values files keep UI sizing consistent across screens and densities. Loading those values from source code is straightforward, but choosing the right API matters because some methods return pixels as float while others round to integers. Understanding that distinction helps you avoid subtle spacing bugs.
Defining Dimensions in XML
A common setup is one dimensions file for spacing, text sizes, and component metrics.
Keep names semantic rather than numeric only. Names like spacing_medium are easier to evolve than padding_16 when design tokens change later.
Loading Dimension Values in Kotlin
Use resources.getDimension when you need a float pixel value.
Use getDimensionPixelSize when you need an integer pixel count suitable for layout params and margins.
getDimensionPixelOffset is another variant that truncates instead of rounding. Most UI code should prefer getDimensionPixelSize for predictable visual results.
Access from Fragments, Views, and Adapters
Context location affects how you access resources, but the principle is the same.
Fragment example:
Custom view example:
RecyclerView adapter example:
Jetpack Compose Equivalent
In Compose, load dimensions using dimensionResource to keep parity with XML tokens.
This makes migration from XML-based layouts smoother because design tokens remain centralized.
Testing and Maintainability
Resource values can differ by qualifiers such as values-sw600dp for tablets. Automated UI tests should verify layouts on at least one phone and one tablet profile so token changes do not break spacing in larger form factors.
Use one dimensions source for each design system tier and avoid scattering hardcoded values in code. Centralized resources improve consistency and simplify theme adjustments.
Java API Equivalent
If parts of your codebase are still Java, the same resource APIs are available with nearly identical semantics.
This makes mixed Kotlin and Java modules easier to maintain while sharing one dimensions source.
When dimensions change as part of a redesign, update XML tokens first and let both Java and Kotlin callers consume the new values automatically. This reduces migration risk and keeps visual behavior aligned across modules.
Common Pitfalls
- Using hardcoded pixel values in source code. Fix by referencing
R.dimenvalues everywhere. - Mixing
getDimensionand integer layout params incorrectly. Fix by usinggetDimensionPixelSizefor size and margin ints. - Naming tokens by numeric values only. Fix by using semantic names tied to UI intent.
- Defining too many near-duplicate dimensions. Fix by consolidating tokens into a manageable scale.
- Forgetting qualifier-specific resources. Fix by testing on different device buckets and adding overrides only when necessary.
Summary
- Define reusable dimensions in XML to keep UI sizing consistent.
- Use
getDimensionfor float pixels andgetDimensionPixelSizefor integer sizes. - Access resources similarly across activities, fragments, views, and adapters.
- Use
dimensionResourcein Compose for shared design tokens. - Centralize and test resource tokens to prevent layout drift.

