Android : Override System Font Size
How to Override System Font Size in Android Application.

Senior Android Engineer from Bangladesh. Love to contribute in Open-Source. Indie Music Producer.
If get to know something new by reading my articles, don't forget to endorse me on LinkedIn
From Android 14 , the system font size can be set to up to 200%. But you might want to keep the application font size according to your requirement.
Here is, how you can override the system font size in your Android application easily.
First : Extension API
Add this extension API in your application.
/**
* Override system font size for current application.
*
* When device font size is set to max/larger size, this API can help
* to override the system font size and set a custom font size.
*
*
* Ex: If the device font size exceeded your expected font size,
* you can override the font size with this API.
*
* @param newBase [Context]
* @param newValue [Float]
* @param overrideValue [Float]
*
* @return [Boolean] to verify success or not.
*/
fun ComponentActivity.overrideFontSize(
newBase: Context?,
newValue: Float,
overrideValue: Float,
): Boolean = try {
Configuration(newBase?.resources?.configuration).apply {
if (this.fontScale > overrideValue) {
this.fontScale = newValue
}
applyOverrideConfiguration(this)
}
true
} catch (e: Exception) {
e.printStackTrace(); false
}
Second : Override
override attachBaseContext(newBase: Context?) API in your BaseActivity.
abstract class BaseActivity() : AppCompactActivity{
//Override and call overrideFontSize api to set custom font size.
override fun attachBaseContext(newBase: Context?) {
val isFontOverwrite: Boolean = overrideFontSize(newBase, 1.3F, 1.3F)
debugLogOnly("Is font override done: $isFontOverwrite")
super.attachBaseContext(LocaleHelper.onAttach(newBase))
}
}
Happy Coding...




