Search code examples
androidandroid-resources

error NotFoundException while trying to retrieve dimension


It's curiously, but I can't get dimen value, so what's wrong? Making Clean or Invalidate caches doesn't help.

dimens.xml:

<dimen name="dialog_width_percent">0.85</dimen>

code:

float percent = context.getResources().getDimension(R.dimen.dialog_width_percent);

Got Resources$NotFoundException


Solution

  • You are not suppose to use float or double as a resource type, check this link: Android float/double resource type

    Basically it explains if you want to use this type you will have to do a kind of "hack". As from the orignal post:

     <item name="float" type="dimen" format="float">9.52</item>
    

    Referencing from java

    TypedValue typedValue = new TypedValue();
    getResources().getValue(R.dimen.my_float_value, typedValue, true);
    float myFloatValue = typedValue.getFloat();
    

    Link to android dev site that explains not being able to use float/double: https://developer.android.com/guide/topics/resources/available-resources