Search code examples
javaandroidresourcesdimensions

Automatic text size scaling by dimension's value


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?


Solution

  • 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