Stay organized with collections
Save and categorize content based on your preferences.
FontScaleConverter
interface FontScaleConverter
A converter for non-linear font scaling. Converts font sizes given in "sp" dimensions to a "dp" dimension according to a non-linear curve.
This is meant to improve readability at larger font scales: larger fonts will scale up more slowly than smaller fonts, so we don't get ridiculously huge fonts that don't fit on the screen.
The thinking here is that large fonts are already big enough to read, but we still want to scale them slightly to preserve the visual hierarchy when compared to smaller fonts.
Summary
Public methods |
abstract Float |
Converts a dimension in "dp" back to "sp".
|
abstract Float |
Converts a dimension in "sp" to "dp".
|
open static FontScaleConverter? |
Finds a matching FontScaleConverter for the given fontScale factor.
|
open static Boolean |
Returns true if non-linear font scaling curves would be in effect for the given scale, false if the scaling would follow a linear curve or for no scaling.
|
Public methods
convertDpToSp
abstract fun convertDpToSp(dp: Float): Float
Converts a dimension in "dp" back to "sp".
convertSpToDp
abstract fun convertSpToDp(sp: Float): Float
Converts a dimension in "sp" to "dp".
forScale
open static fun forScale(fontScale: Float): FontScaleConverter?
Finds a matching FontScaleConverter for the given fontScale factor. Generally you shouldn't need this; you can use android.util.TypedValue#applyDimension(int, float, DisplayMetrics) directly and it will do the scaling conversion for you. Dimens and resources loaded from XML will also be automatically converted. But for UI frameworks or other situations where you need to do the conversion without an Android Context, you can use this method.
This method is safe to call from any thread.
Return |
FontScaleConverter? |
a converter for the given scale, or null if non-linear scaling should not be used. |
isNonLinearFontScalingActive
open static fun isNonLinearFontScalingActive(fontScale: Float): Boolean
Returns true if non-linear font scaling curves would be in effect for the given scale, false if the scaling would follow a linear curve or for no scaling.
Example usage: isNonLinearFontScalingActive(getResources().getConfiguration().fontScale)
This method is safe to call from any thread.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# FontScaleConverter\n\nAdded in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nFontScaleConverter\n==================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/content/res/FontScaleConverter \"View this page in Java\") \n\n```\ninterface FontScaleConverter\n```\n\n|---------------------------------------------|\n| [android.content.res.FontScaleConverter](#) |\n\nA converter for non-linear font scaling. Converts font sizes given in \"sp\" dimensions to a \"dp\" dimension according to a non-linear curve.\n\nThis is meant to improve readability at larger font scales: larger fonts will scale up more slowly than smaller fonts, so we don't get ridiculously huge fonts that don't fit on the screen.\n\nThe thinking here is that large fonts are already big enough to read, but we still want to scale them slightly to preserve the visual hierarchy when compared to smaller fonts.\n\nSummary\n-------\n\n| Public methods ||\n|------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html) | [convertDpToSp](#convertDpToSp(kotlin.Float))`(`dp:` `[Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html)`)` Converts a dimension in \"dp\" back to \"sp\". |\n| abstract [Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html) | [convertSpToDp](#convertSpToDp(kotlin.Float))`(`sp:` `[Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html)`)` Converts a dimension in \"sp\" to \"dp\". |\n| open static [FontScaleConverter](#)? | [forScale](#forScale(kotlin.Float))`(`fontScale:` `[Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html)`)` Finds a matching FontScaleConverter for the given fontScale factor. |\n| open static [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [isNonLinearFontScalingActive](#isNonLinearFontScalingActive(kotlin.Float))`(`fontScale:` `[Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html)`)` Returns true if non-linear font scaling curves would be in effect for the given scale, false if the scaling would follow a linear curve or for no scaling. |\n\nPublic methods\n--------------\n\n### convertDpToSp\n\nAdded in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun convertDpToSp(dp: Float): Float\n```\n\nConverts a dimension in \"dp\" back to \"sp\". \n\n### convertSpToDp\n\nAdded in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun convertSpToDp(sp: Float): Float\n```\n\nConverts a dimension in \"sp\" to \"dp\". \n\n### forScale\n\nAdded in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen static fun forScale(fontScale: Float): FontScaleConverter?\n```\n\nFinds a matching FontScaleConverter for the given fontScale factor. Generally you shouldn't need this; you can use android.util.TypedValue#applyDimension(int, float, DisplayMetrics) directly and it will do the scaling conversion for you. Dimens and resources loaded from XML will also be automatically converted. But for UI frameworks or other situations where you need to do the conversion without an Android Context, you can use this method. \nThis method is safe to call from any thread.\n\n| Parameters ||\n|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `fontScale` | [Float](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-float/index.html): the scale factor, usually from [Configuration.fontScale](/reference/kotlin/android/content/res/Configuration#fontScale:kotlin.Float). |\n\n| Return ||\n|--------------------------|------------------------------------------------------------------------------------|\n| [FontScaleConverter](#)? | a converter for the given scale, or null if non-linear scaling should not be used. |\n\n### isNonLinearFontScalingActive\n\nAdded in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen static fun isNonLinearFontScalingActive(fontScale: Float): Boolean\n```\n\nReturns true if non-linear font scaling curves would be in effect for the given scale, false if the scaling would follow a linear curve or for no scaling.\n\nExample usage: `isNonLinearFontScalingActive(getResources().getConfiguration().fontScale)` \nThis method is safe to call from any thread."]]