Android: How to change app font size from code

13,455

Solution 1

It seems like I cant change app font size and I have to change font size for individual element seperateley.

Solution 2

You can alter text size for textviews with

textview.setTextSize(int_value);

Is this what you meant?

Solution 3

Call setTextSize() on each TextView (or other widgets that inherit from TextView, such as Button).

Solution 4

The only way I can think of is to extend TextView and make them check a global textSize variable on every onDraw(). It should not add too much overhead to the drawing.

Share:
13,455
binW
Author by

binW

I am a software developer from Pakistan. I like cooking, watching movies and site seeing.

Updated on June 04, 2022

Comments

  • binW
    binW about 2 years

    I have a requirement where I need to change the application font size based on user provided values. How can I do that? I have googled but it seems that this can only be done is from XML layouts.

  • binW
    binW about 13 years
    I want the text size for whole application to change i.e all views in all activities. Is there any setting at application level that I can set/change to control the whole app's text size?
  • makata
    makata about 10 years
    It is not me who voted down. But why did not accept @CommonsWare's answer (as you have put nothing new)?
  • Inzimam Tariq IT
    Inzimam Tariq IT over 6 years
    this is not theme.xml, this is styles.xml
  • Zaid Mirza
    Zaid Mirza over 4 years
    Is there any better way? I think we can update font for whole application by updating app context and restart the app but it's better to update the font size on real time(without restarting app). It could be possible by inheriting each widget class and use ViewModel initialized with App context and observe the value inside new derived classes.? Is there any better way ?
  • CommonsWare
    CommonsWare over 4 years
    @ZaidMirza: "I think we can update font for whole application by updating app context and restart the app" -- I do not know what you mean by "updating app context" or by "restart the app". "It could be possible by inheriting each widget class and use ViewModel initialized with App context and observe the value inside new derived classes.?" -- IMHO, that is an unwise architecture. "Is there any better way ?" -- I am not a fan of the "requirement" from the question. If I had to do this today, I'd probably use data binding. In 2021 and beyond, Jetpack Compose should simplify this a lot.
  • Zaid Mirza
    Zaid Mirza over 4 years
    @CommonsWare by "update app context" means as we do create new or update context when changing the locale of app runtime and sometime we have to restart the app to reflect the language changes in whole application. Same thing we can do using context.resources.fontScale(newValue). I hope you get my point.
  • Zaid Mirza
    Zaid Mirza over 4 years
    But I want to avoid restart the app and want to change font size in whole application at runtime using SeekBar that is why I was thinking to use ViewModel concept to dispatch event of change value for font size. Observer will be the derived class like "MyTextView","MyRadioButton".
  • CommonsWare
    CommonsWare over 4 years
    @ZaidMirza: "Observer will be the derived class like "MyTextView","MyRadioButton"." -- I feel that this is an unwise architecture. "Same thing we can do using context.resources.fontScale(newValue)" -- AFAICT, that does not exist. You could try createConfigurationContext(), but I suspect that you will encounter problems.
  • Zaid Mirza
    Zaid Mirza over 4 years
    yes , I talking about this createConfigurationContext(), Sorry missed to mention it and you are right I could face problems as we may need to restart the application in this case. So, final words are that we don't have any optimized solution for changing font size at runtime, till now.
  • CommonsWare
    CommonsWare over 4 years
    @ZaidMirza: "we don't have any optimized solution for changing font size at runtime" -- agreed. Android assumes that font decisions are largely static, driven by your choice of a font size at compile time combined with the user's font scale. And, to the extent that font sizes are to change at runtime, usually that is based on text length (autosize). Just as Android is not set up to allow apps to change locale, it is not set up to allow apps to change font size. Both can be achieved, but you are "swimming against the current", as it were.