I am trying to add float to my dimens.xml file.
I read the following SO answer . When I tried the solution, I got the exception described in the comments. I am trying to understand why this exception is thrown.
For completeness, XML is provided here:
<item name="zoom_level" format="float" type="dimen">15.0</item>
Here is the code that explodes:
final float zoom = this.getResources().getDimension(R.dimen.zoom_level);
I moved to the Android source code 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 some reason value.type != TypedValue.TYPE_DIMENSION . I don’t have a fully configured source for Android, so I can’t easily add the Log.w("YARIAN", "value type is " + value.type)' instruction Log.w("YARIAN", "value type is " + value.type)' .
Then I jumped into getValue , and the call chain looks like this:
Resources.getValue -> AssetManager.getResourceValue -> AssetManager.loadResourceValue
loadResourceValue is a native method, and this is where my digging falls apart.
Does anyone know the best way to understand what is happening?
I also noticed that Resources has a TypedValue.TYPE_FLOAT and TypedValue.TYPE_DIMENSION . But in XML I cannot write type="float" .
The work described in the comments is to use type=string , and then use Float.parse to get a float. It's necessary? Why or why not?