Darksymphony
Darksymphony

Reputation: 2683

Android do not increase text in application when device font size changes

I have an android app, where I use a list of items, for example I have a thumbnail on the left, and some text to right of it.

Everything works fine, but I noticed a problem, when the user increases the font size for his device (not in application, but generally for his device as he is an older person, needs to have bigger letters). As each mobile device has in settings a possibility to change font size - small. medium, large, very large etc.)

However, this change increase also the font size in my application, and then the list of items looks very bad, as the text right of the image comes to multiple lines or even if stays on one line, it looks very bad as it's zoomed, because the device text size is increased.

Normally I use text size in SP (e.g.16sp):

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16sp"
</TextView>

Is there a way to have my app not affected by the device font size settings, as my app has it's own font sizes or there is nothing to do ?

Now if someone needs to have big fonts on his device, he can't use the app normally.

Upvotes: 2

Views: 5246

Answers (2)

VasiliyT
VasiliyT

Reputation: 521

You can override in base activity

override fun attachBaseContext(newBase: Context?) {
    super.attachBaseContext(newBase)
    val config = Configuration(newBase?.resources?.configuration)
    config.fontScale = 1.0f
    applyOverrideConfiguration(config)
}

Java:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(newBase);
    Configuration config = new Configuration(newBase.getResources().getConfiguration());
    config.fontScale = 1.0f;
    applyOverrideConfiguration(config);
}

Upvotes: 6

Sam
Sam

Reputation: 263

Use dp instead of sp for fontsize of TextView. It will not affect the size of text even after user changes the phone font size because sp is scaled by user's font size preference, other than that dp and sp have same default values.

Upvotes: 4

Related Questions