Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use variables in XML for Android?

Tags:

android

xml

Instead of using this following line for each textview:

android:textSize="40sp"

Am I able to replace 40sp with a variable?

As a side question, I've been recommended to use SP units as it goes by user defined settings. Is this a best practice?

like image 381
Ayleanna Avatar asked Sep 02 '12 13:09

Ayleanna


1 Answers

Am I able to replace 40sp with a variable?

You can either use a dimension resource (e.g., android:textSize="@dimen/someSize"), or use a style that does this (per AlexN's answer), or set the size at runtime via setTextSize().

I've been recommended to use SP units as it goes by user defined settings. Is this a best practice?

On newer versions of Android, the user can change the base font size via the Settings application. By using sp for units, your font sizes will scale along with the base font size. If you use other units (e.g., dp), your font size will remain the same regardless of what the user has in Settings. It is impossible to say whether any given use of sp is a "best practice" or not -- I am sure that there are cases where allowing the font size to change would be a bad thing. By default, though, sp is probably the right starting point.

like image 166
CommonsWare Avatar answered Sep 19 '22 23:09

CommonsWare