0

I'm trying to implement a feature where the user can activate 'Dyslexia'mode, which changes the font of the entire application to one saved in res as a .tff file.

From reading around, this seems to be a very difficult task. I've understood that it's possible to use a custom font for the entire application (from here), but from my understanding of this example, it's not suitable for switching the font through an onClick method.

Is there a way of doing this triggered by a button?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kevin H
  • 11
  • 2

1 Answers1

0

You can extend TextView with your own class, using it everywhere in your project.

public class MyTextView extends TextView {

   public MyTextView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
       init(context);
   }

   public MyTextView(Context context, AttributeSet attrs) {
       super(context, attrs);
       init(context);
   }

   public MyTextView(Context context) {
       super(context);
       init(context);
   }

   private void init(Context context) {
       Typeface tf =
       Typeface.createFromAsset(context.getAssets(), getFontFromPreferences(context));
       setTypeface(tf);
   }

   private String getFontFromPreferences(Context context) {
       SharedPreferences preferences = context.getSharedPreferences("myPrefs", Context.MODE_PRIVATE);   
       String fontString = preferences.getString("my_font_pref_key", "Default.tff");
       return fontString;
   }
}

In your settings button just set in the SharedPreferences the new font.

Roberto Martucci
  • 1,237
  • 1
  • 13
  • 21
  • Would I need to do this for Button as well? – Kevin H Nov 27 '17 at 15:57
  • You could also use a TextView to implement a Button, with a proper background, and so "MyTextView" would work the same way. But if you anyway want to use a Button, then yes, you need to extend also this one. – Roberto Martucci Nov 27 '17 at 16:54