Issue
I am trying to add a float to my dimens.xml
file.
I was reading the following SO answer. When I tried the solution, I got the exception described in the comments. I am trying to figure out why that exception is thrown.
For completeness here is the XML:
<item name="zoom_level" format="float" type="dimen">15.0</item>
Here is the code that blows up:
final float zoom = this.getResources().getDimension(R.dimen.zoom_level);
I jumped into the Android source, and here is the method definition for getDimension:
public float getDimension(int id) throws NotFoundException {
synchronized (mTmpValue) {
TypedValue value = mTmpValue;
getValue(id, value, true);
if (value.type == TypedValue.TYPE_DIMENSION) {
return TypedValue.complexToDimension(value.data, mMetrics);
}
throw new NotFoundException(
"Resource ID #0x" + Integer.toHexString(id) + " type #0x"
+ Integer.toHexString(value.type) + " is not valid");
}
}
So for whatever reason value.type != TypedValue.TYPE_DIMENSION
. I do not have my Android source completely set up so I cannot easily add a Log.w("YARIAN", "value type is " + value.type)'
statement in there.
I then jumped into getValue
and the chain of calls seems to be:
Resources.getValue -> AssetManager.getResourceValue -> AssetManager.loadResourceValue
loadResourceValue
is a native method and here is where my digging falls apart.
Anybody know what the best way to understand what's going is?
I also noticed that Resources
has a TypedValue.TYPE_FLOAT
and TypedValue.TYPE_DIMENSION
. But in XML, I cannot write type="float"
.
The work around described in the comments is to use type=string
and then use Float.parse
to get the float. Is this necessary? Why or why not?
Solution
I know it's a late answer but you should use TypedValue#getFloat() instead of parsing the String to a float like you suggested.
XML:
<item name="float_resource" format="float" type="raw">5.0</item>
Java:
TypedValue out = new TypedValue();
context.getResources().getValue(R.raw.float_resource, out, true);
float floatResource = out.getFloat();
You can put fraction
, raw
or string
as the type
if you prefer, this only corresponds to the resource class in R
.
Answered By - Rich
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.