Add floating point value to android resources/values
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Android resource files are great for configuration values, but plain floating-point numbers are a little less obvious than strings or integers. The main question is whether your number represents a real UI dimension, a ratio, or a raw float constant, because Android handles those cases differently.
Use dimen When the Value Is a Measurement
If the floating-point value represents a physical UI size, such as padding or radius, store it as a dimension with units.
Then read it in code:
This is the right solution for layout and typography because Android understands units such as dp and sp and converts them appropriately.
Use fraction for Ratios and Percentages
If the value is a ratio rather than a size, use a fraction resource.
Read it with:
This is a better fit for proportions such as 0.8 of screen width than pretending the value is a dimension.
Raw Float Values Need an item
If you truly need a raw float constant, define it with an item resource and a float format.
Then retrieve it as a float:
This pattern is for unitless configuration values, not for actual view sizes.
The key distinction is important:
- Use
dimenwith units for measurements. - Use
fractionfor proportions. - Use a float-style
itemonly for genuine raw float settings.
Why Choosing the Right Type Matters
Android resource types are not interchangeable documentation labels. They affect how the framework parses and returns the value.
For example, a layout size should not be stored as a raw float because a raw float does not carry dp or sp semantics. Likewise, a unitless coefficient should not be stored as a dimen with fake units just to force it into the resource system.
Choosing the correct type keeps the resource self-explanatory and makes the retrieval API match the actual meaning.
Practical Example
Suppose you have a custom animation configuration:
Then in code:
Now each resource reflects what it actually represents.
API-Level Awareness
If you choose a raw float resource, make sure the retrieval API you plan to use is available in the app's supported environment. If the value is really a UI measurement, a unit-based dimen is often the simpler and more portable choice anyway.
Common Pitfalls
- Storing a dimensionless coefficient as a
dimenwith fake units. - Using a raw float where a
dporspdimension should have been used. - Forgetting that fractions are often a better representation for percentages.
- Reading a resource with the wrong API, such as treating a dimension like a raw float.
- Hiding the meaning of the value by choosing a resource type only because it compiles.
Summary
- Pick the resource type based on what the number means, not just on the fact that it has decimals.
- Use
dimenwith units for measurements. - Use
fractionfor ratios and percentages. - Use a float-style
itemonly for real unitless float constants. - Match the retrieval API to the resource type so the code stays correct and readable.

