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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With