How to change the font size of text displayed at run time in the styles.xml in my application depending on the screen size of the device used.
3 Answers
I had the same problem. I wanted to get the same aspect text ratio on every screen. No dp nor sp worked for me. The only solution that works for me is to create a dummy textbox (not visible) at startup then get the textsize and multiply for some ratio (depending on what you actually want). This value should be used to set text size dynamically to your controls. At least it works for me because the same aspect remains on every different screen.
-
tried spomething like this and it worked...got the wth and ht of the screen in the onCreate bundle and multipied by a ratio – Aparna Apr 08 '15 at 16:12
-
what is ratio here ? – amity Aug 11 '17 at 13:01
you need to add in the text size via dimens.xml files depending on screen size.....take a look at this following Question on SO - Text size and different android screen sizes

- 1
- 1

- 986
- 2
- 12
- 40
You can go through this, I used the below method to scale texts on different screen resolutions and sizes.
To define a range of text sizes and a dimension programmatically through the support library, call the TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(TextView textview, int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)
method. Provide the text view on which it needs to be applied,the maximum value, the minimum value, the granularity value, and any TypedValue dimension unit (like TypedValue.COMPLEX_UNIT_SP
).

- 1,108
- 12
- 20