Search code examples
androidfontstruetypeandroid-fonts

How to apply Font type for a whole application in Android?


I want to apply Font type without using XML. By code, I want to change Font type and it should be able to apply for a whole application, when I click Arial else Times New Roman etc.


Solution

  • I would reccomend having a list preference, in a preference screen. You can then set the fonts from that. i.e:

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); 
    String font = sp.getString("font","-1")
    if(font.equals("timesroman")) {
    TypeFace  typeFace = Typeface.createFromAsset(getAssets(), "timesnewroman.ttf");
    }
    else if(font.equals("arial")) {
    TypeFace  typeFace = Typeface.createFromAsset(getAssets(), "arial.ttf");
    }
    TextView view= (TextView) findViewById(R.id.view);
    view.setTypeface(typeFace)