Add Floating Point Value to Android Resources/Values

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.

Android: how to conditionally show floating point part in string resource?

As per your requirement, you can simply achieve this by typecasting. remove the template from string resources.

fun getLiterString(value : Float) : String
{
if(value.toInt().toFloat() == value)
{
return getString(R.string.liter_d, value.toInt())
}
else
{
return getString(R.string.liter_f, value)
}
}

string.xml

<string name="liter_d">%d liter</string>
<string name="liter_f">%.1f liter</string>

If you want to keep string resources,
You can make two templates for liter or else you have to replace ".0" from the output.

Example : https://pl.kotl.in/3poIS2lLJ

Calculator that accepts floating point values

Change all of

Integer.parseInt()

to

Double.parseDouble()

and all of the variable declarations to double instead of int.

The app crashes with

java.lang.NumberFormatException

when you try to parse a value like 1.1 to Integer.

Storing decimals in resource folders

Based on your comment, I understand your questions as a performance question.

Saving and using an int in resources instead of a float should not be noticeable in the performance at all. By that note I think saving an int and then dividing by 100 all the time would be silly, when you could just as well save a float. Also noting that when you divide your int it will get implicitly converted to a double anyway.

You say that you use the value for an alpha value, and you might only need the precision 1 to 100, using a float for that kind of precision is very reasonable, and also reasonable for an alpha value.

You might want to read Float or Double?

Two floating point addition is not changing result

For any floating point value, in combination with a (much) smaller adding delta, there is a value from which adding the delta results in a value which is so "similar" to the previous one, that the representation in float is identical.

Your code is practically tailor-made to find that value.

So, while at first adding delta to 2000 and a few following values results in visible changes, your code will sooner or later (quite soon actually) reach the special value. Which I assume in your case is 2048.

2048.0 and 2048.0001 have no different representation in float. Hence the adding has no effect on the variable.



Related Topics



Leave a reply



Submit