3

There is a system wide "Font size" setting allowing to resize text.

It generally works well, but there are cases where something special happens and specific part of view should ignore this setting and font size should remain at constant size.

I am aware about how to prevent system font-size changing effects to android application?

But accepted answer with dp is nowadays (Android 10) not working ( https://stackoverflow.com/a/21546897/4130619 )

https://stackoverflow.com/a/57225687/4130619 removes scaling in entire activity, https://stackoverflow.com/a/39346113/4130619 removes scaling in the entire app.

Affected code, with dp attempt already used (and failed):

            <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/compassNorth"
            android:layout_alignParentTop="true"
            android:text="@string/compass_north_one_letter"
            android:textColor="#000"
            android:textSize="10dp"/>

N (compass_north_one_letter) is changing size anyway:

enter image description here

reducing activity
  • 1,985
  • 2
  • 36
  • 64
  • 2
    "But accepted answer with dp is nowadays (Android 10) not working" -- screenshots would help. – CommonsWare Jun 17 '20 at 22:37
  • @CommonsWare I am not sure how screenshot would help - text is changing size while I want to stop that. Maybe it would help in case where I somehow hallucinated text size change? I will add it anyway. – reducing activity Jun 18 '20 at 09:10
  • Interesting (on Android 8.1 this *does not happen*). Have you used the Layout Inspector to see what the TextView is really rendering? – Martin Marconcini Jun 18 '20 at 10:02
  • 2
    "Maybe it would help in case where I somehow hallucinated text size change?" -- no, but I need to see what you are seeing. I took your `TextView`, put it in a scrap project, ran it on a Pixel 4 running Android 10, and I do not get the effect that you are seeing. I jacked the font scale up to the max, and the N is the same size as it was with a font scale to normal, even after terminating the process. So, create a scrap project, put your `TextView` in it, and try running that on the device that you used for those screenshots. If the problem exists, it's probably a hardware difference. – CommonsWare Jun 18 '20 at 12:44
  • You can use custom font to your textview so it will not be affected by any changes. Possible solution https://stackoverflow.com/a/27588966/9044222 – JexSrs Jun 22 '20 at 10:28
  • @GeorgeAndredakis Are you sure that only Roboto is scaled and all other fonts remain unscaled? – reducing activity Jun 22 '20 at 21:23
  • In my app using api level 25 worked perfectly – JexSrs Jun 24 '20 at 12:45

2 Answers2

4

Try autoSizeTextType, that make text size auto scale by available view size. https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview

It allows you to specify max size, preset sizes so you could have more control. I don't know entire your layout so cannot test actually but from your snippet, for example,

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/compassNorth"
            android:layout_alignParentTop="true"
            android:text="@string/compass_north_one_letter"
            android:textColor="#000"
            app:autoSizeTextType="uniform"
        />

Also, I think you need to specify layout_height with specific size to limit view size for auto sizing. If it cannot work, please try app:autoSizeMaxTextSize, or granularly sizing as well.

ytRino
  • 1,450
  • 15
  • 28
-1

In your case I would suggest using an image (dirty solution) because it's pretty straightforward in what you're trying to do. You could even create the image on the go but only if you need to go that deep.

Vendetta8247
  • 592
  • 1
  • 5
  • 30
  • 1
    why would he sacrifice localization, scaling, rendering, etc. just "for this case"? The only reason to suggest this, is if you are absolutely confident you know what's going on, and that there's no other way around it. Which I doubt is the case here and even if it were, I'd rather have the OP handle font scaling correctly (to accomodate for accessibility) than bloat his/her app with PNGs... – Martin Marconcini Jun 18 '20 at 10:01
  • @MartinMarconcini it's obviously a button that shows direction so yeah, I suspected that it would be an easy dirty solution as I stated. It doesn't "bloat" app if you use one extra drawable. But it was just a suggestion, not really the best idea, rather something that could be a quick fix for this specific scenario – Vendetta8247 Jun 18 '20 at 11:33
  • Still, even if bad solution at least it is a solution. I hoped for something better but... – reducing activity Jun 21 '20 at 09:58