-1

I want to have a setting activity where the user be able to change the font size of the text for the entire app ... I also only have one textview which makes the work easy but I can't figure it out by myself.

for example I have an activity where it displays the texts and it's called (Contentactivity)

and then there will be another activity named (Settingactivity ) where from here the user be able to change the font size of the text of the Contentactivity .

I hope it's clear and thank you very much for your help .

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Khamkhor
  • 29
  • 8
  • Set text size in xml as "sp" not "dp". and after that change text size from setting – ashish May 16 '17 at 13:18
  • 1
    Thank you my friend @Phil3992 that's what I am looking for and I'm gonna try it out :) – Khamkhor May 16 '17 at 13:21
  • @ashish I always use sp format but I want to set a font size options programmatically . Thank you :) – Khamkhor May 16 '17 at 13:23
  • You can change system font using below code `Settings.System.putFloat(getBaseContext().getContentResolver(), Settings.System.FONT_SCALE, (float) 1.0);` and after this give permission in menifest file `` – ashish May 16 '17 at 13:30

3 Answers3

3

Here is how I did it :

  1. Create a style for each wanted text size :

    <style name="RegularSizedText"> <item name="text_size">20sp</item> </style> <style name="LargeSizedText"> <item name="text_size">35sp</item> </style>

  2. Create an enumeration/map of text sizes, associating each style ID with a number (R.style.RegularSizedText <=> 0, R.style.LargeSizedText <=> 1...) - you must not change it afterwards or it will break the settings when updating the app, you can only append parameters but never remove/insert any
  3. In your shared properties (or any other parameters storage system), put the number ID of the style of the current font size - add it to your settings activity
  4. In each Activity of your app, put this piece of code in the onCreate method : getTheme().applyStyle(myStyle, false); Where myStyle is the style ID corresponding to the stored parameter (so if the user selected the font parameter #0 it will be R.style.RegularSizedText)

You must never store the style ID directly as it will be broken when updating the app (the Android R IDs are not the same from a build to another).

natinusala
  • 596
  • 5
  • 21
0

Just use sp instead of dp when setting your size. This will guarantee that your text always adjust to the screen size and user's preference in Settings.

Check this SO Answer for more detail.

Community
  • 1
  • 1
Abbas
  • 3,529
  • 5
  • 36
  • 64
0

You can change system font using below code

Settings.System.putFloat(getBaseContext().getContentResolver(), Settings.System.FONT_SCALE, (float) 1.0);

and after this give permission in menifest file

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>

ashish
  • 848
  • 6
  • 16