Add floating point value to android resources/values
There is a solution:
<resources> <item name="text_line_spacing" format="float" type="dimen">1.0</item></resources>
In this way, your float number will be under @dimen. Notice that you can use other "format" and/or "type" modifiers, where format stands for:
Format = enclosing data type:
- float
- boolean
- fraction
- integer
- ...
and type stands for:
Type = resource type (referenced with R.XXXXX.name):
- color
- dimen
- string
- style
- etc...
To fetch resource from code, you should use this snippet:
TypedValue outValue = new TypedValue();getResources().getValue(R.dimen.text_line_spacing, outValue, true);float value = outValue.getFloat();
I know that this is confusing (you'd expect call like getResources().getDimension(R.dimen.text_line_spacing)
), but Android dimensions
have special treatment and pure "float" number is not valid dimension.
Additionally, there is small "hack" to put float number into dimension, but be WARNED that this is really hack, and you are risking chance to lose float range and precision.
<resources> <dimen name="text_line_spacing">2.025px</dimen></resources>
and from code, you can get that float by
float lineSpacing = getResources().getDimension(R.dimen.text_line_spacing);
in this case, value of lineSpacing
is 2.024993896484375
, and not 2.025
as you would expected.
As described in this link http://droidista.blogspot.in/2012/04/adding-float-value-to-your-resources.html
Declare in dimen.xml
<item name="my_float_value" type="dimen" format="float">9.52</item>
Referencing from xml
@dimen/my_float_value
Referencing from java
TypedValue typedValue = new TypedValue();getResources().getValue(R.dimen.my_float_value, typedValue, true);float myFloatValue = typedValue.getFloat();
All the solutions suggest you to use the predefined float value through code.
But in case you are wondering how to reference the predefined float value in XML (for example layouts), then following is an example of what I did and it's working perfectly:
Define resource values as type="integer"
but format="float"
, for example:
<item name="windowWeightSum" type="integer" format="float">6.0</item><item name="windowNavPaneSum" type="integer" format="float">1.5</item><item name="windowContentPaneSum" type="integer" format="float">4.5</item>
And later use them in your layout using @integer/name_of_resource
, for example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:weightSum="@integer/windowWeightSum" // float 6.0 android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="@integer/windowNavPaneSum" // float 1.5 android:orientation="vertical"> <!-- other views --> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="@integer/windowContentPaneSum" // float 4.5 android:orientation="vertical"> <!-- other views --> </LinearLayout></LinearLayout>