Resources$NotFoundException: resource ID not valid. Why?

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.


I just ran into this problem too, and though the error message isn't too helpful, I realized my problem was that I was putting just a float value in my resource file and didn't specify a measurement. Switching 15.0 to 15.0dp for instance would avoid the problem and allow you to still use a regular dimension resource.


There's now Resources.getFloat (from API 29) and ResourcesCompat.getFloat:

val zoomLevel: Float = ResourcesCompat.getFloat(resources, R.dimen.zoom_level)

You can leave your zoom_level XML as it is in the question.