0

I was looking for a complete answer over 2 days searching here and there but no chance. I'm new in android by the way .
I want 3 different fonts in my app which will be in Settings activity and there will be three buttons or radio buttons for each font so that users can choose the font they like and after tapping the save button the font of all activities will change.
Here is what I know:
place fon.ttf in fonts folder in assets I know how to set a font to a textView using typeface.
I see the answer provided here Android: Want to set custom fonts for whole application not runtime
but nothing teaches me what I need learn.
please give me a code if it is possible
or tel me what should I do
thanks

Community
  • 1
  • 1
unknown
  • 29
  • 1
  • 7

2 Answers2

0

In your settings activity, when the user selects a font size, save it to preferences as an integer

Then, in your activity where you need the font size, grab the value from prefs, and set the text size accordingly.

Here's an example

MainActivity.java

public class MainActivity extends Activity {
    public int fontSize = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        //grab all textViews
        TextView tv = (TextView) findViewById(R.id.your_text_view_id);

        SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(MainActivity.this);     

        fontSize = prefs.getInt("pref_fontSize", 12);

        tv.setTextSize(fontSize);

    }

}

If you need further assistance, post your Settings code, and your Activity code

jb15613
  • 359
  • 4
  • 12
0

Put your different fonts in asset folder.

Now when you want to change your font in app, select the desired font from asset. Make sure fonts are in ttf format. Here is a example -

public void setFont(String FONTNAME) { //Just the font name. Don't add ttf at end

    Typeface tf = Typeface.createFromAsset(getAssets(),FONTNAME + ".ttf");  
    //Select font from asset folder
    //Make sure that the font with that name exists in asset folder

    textview.setTypeface(tf);
    //Set it to textview or any text field
    //Do it muliple times if you have many textfields

    //Also create different methods in other activities to set the font to them
    //and then call then from here

}

If you want to display the same font to the user everytime they open the app, use shared preference to save the font name. Then in onCreate method, check if the font exists through shared preferance and if it does, then call the above method to set it.

Confuse
  • 5,646
  • 7
  • 36
  • 58
  • you could also try to add an id to the paramteters of this function, pass it the font and the id of the view, put it in a utility class and use it all over the app – jb15613 Oct 16 '14 at 00:10