0

I have set of dimension files like:

\res\values-normal-xxhdpi\dimens.xtml

<!-- Default screen margins, per the Android Design guidelines. -->
<string name="dimention_id">normal-xxhdpi</string>
<dimen name="activity_margin">8dp</dimen>
<dimen name="big_text_size">8sp</dimen> 
<dimen name="mid_text_size">5sp</dimen> 
<dimen name="small_text_size">3sp</dimen>
<dimen name="logo_size">100dp</dimen>
<dimen name="button_height">55dp</dimen>
<dimen name="button_width">200dp</dimen>
<dimen name="panel_counter_width">190dp</dimen>

When I loading this dimensions by Context.getResources().getDimension(R.dimen.mid_text_size) I receive "mid_text_size" is equal to 20.

I sure that it is correct file because changing of value 5 to other value will appear changes in the application.

Why it scaling up to 20 from 5?

Charuක
  • 12,953
  • 5
  • 50
  • 88
Yury Finchenko
  • 1,035
  • 13
  • 19

1 Answers1

1

The documentation of the function Resources.getDimenstion(int) states:

Resource dimension value multiplied by the appropriate metric.

So probably, if you check DisplayMetrics.scaledDensity it would show the value of 4.

Remember that sp is a relative value, that is supposed to assure, that the font looks much like the same on all screen sizes and dimesions and resolutions.

I suggest you reading the theory of units used in Android environment

Community
  • 1
  • 1
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
  • Yes it solves my problem. float dimension = getContext().getResources().getDimension(R.dimen.mid_text_size); DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); float scale_index = metrics.scaledDensity; dimension = dimension / scale_index; Interesting that when we set dimention value in layout it working simple for every avalable layout file. But when we using getDimentions() function we must test metrics.scaledDensity value. Thank you. – Yury Finchenko Jan 12 '17 at 15:37