I want to specify my own text size in my application, but I am having a problem doing this.
When I change the font size in the device settings, the font size of my application TextView
also changes.
I want to specify my own text size in my application, but I am having a problem doing this.
When I change the font size in the device settings, the font size of my application TextView
also changes.
Also note that if the textSize is set in code, calling
textView.setTextSize(X)
interprets the number (X) as SP. UsesetTextSize(TypedValue.COMPLEX_UNIT_DIP, X)
to set values in dp.simple way to prevent the whole app from getting effected by system font size is to updateConfiguration using a base activity.
Actually, Settings font size affects only sizes in
sp
. So all You need to do - definetextSize
indp
instead ofsp
, then settings won't change text size in Your app.Here's a link to the documentation: Dimensions
However please note that the expected behavior is that the fonts in all apps respect the user's preferences. There are many reasons a user might want to adjust the font sizes and some of them might even be medical - visually impaired users. Using
dp
instead ofsp
for text might lead to unwillingly discriminating against some of your app's users.i.e:
Using font size in DPs instead of SPs in
textSize
solves this.Use the
dimension
type of resources like you usestring
resources (DOCS).In your
dimens.xml
file, declare your dimension variables:Then you can use these values like this:
You can declare different
dimens.xml
files for different types of screens. Doing this will guarantee the desired look of your app on different devices.When you don't specify
android:textSize
the system uses the default values.If units used are SP, not DP, and you want to override system font scaling, all you do is override one method in activity - see this post.
resources.updateConfiguration is deprecated and buggy (on Oreo I had issues with formatted text).