android
resources
values
floating point
xml

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.

xml
1<resources>
2    <dimen name="card_corner_radius">3.5dp</dimen>
3    <dimen name="title_text_size">14.5sp</dimen>
4</resources>

Then read it in code:

kotlin
val radius = resources.getDimension(R.dimen.card_corner_radius)
val textSize = resources.getDimension(R.dimen.title_text_size)

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.

xml
<resources>
    <fraction name="drawer_width_ratio">80%</fraction>
</resources>

Read it with:

kotlin
val ratio = resources.getFraction(R.fraction.drawer_width_ratio, 1, 1)

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.

xml
<resources>
    <item name="spring_damping" type="dimen" format="float">0.85</item>
</resources>

Then retrieve it as a float:

kotlin
val damping = resources.getFloat(R.dimen.spring_damping)

This pattern is for unitless configuration values, not for actual view sizes.

The key distinction is important:

  • Use dimen with units for measurements.
  • Use fraction for proportions.
  • Use a float-style item only 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:

xml
1<resources>
2    <item name="card_spring_stiffness" type="dimen" format="float">250.0</item>
3    <item name="card_spring_damping" type="dimen" format="float">0.78</item>
4    <fraction name="card_expanded_ratio">65%</fraction>
5    <dimen name="card_elevation">6dp</dimen>
6</resources>

Then in code:

kotlin
1val stiffness = resources.getFloat(R.dimen.card_spring_stiffness)
2val damping = resources.getFloat(R.dimen.card_spring_damping)
3val ratio = resources.getFraction(R.fraction.card_expanded_ratio, 1, 1)
4val elevation = resources.getDimension(R.dimen.card_elevation)

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 dimen with fake units.
  • Using a raw float where a dp or sp dimension 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 dimen with units for measurements.
  • Use fraction for ratios and percentages.
  • Use a float-style item only for real unitless float constants.
  • Match the retrieval API to the resource type so the code stays correct and readable.

Course illustration
Course illustration

All Rights Reserved.