0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62

4 Answers4

1

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

Nigel Brown
  • 440
  • 2
  • 13
0

0.85 is not a valid dimen. Use fraction

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

float percent = context.getResources().getFraction(R.fraction.dialog_width_percent, 1, 1);
egoldx
  • 1,490
  • 1
  • 9
  • 14
0

Try 0.85dp as follow

<dimen name="dialog_width_percent">0.85dp</dimen>
Abner Escócio
  • 2,697
  • 2
  • 17
  • 36
0

Dimensions expect dp use fraction instead.

<fraction name="dialog_width_percent" type="fraction">85%</fraction>

You can then call it in your activity like this:

float percent = context.getFraction(R.fraction.dialog_width_percent, 1, 1);

hopeman
  • 2,778
  • 3
  • 18
  • 33