0

I am trying to change the font of all my button in onCreate() method there is a way to do that ?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Typeface buttontypeface = Typeface.createFromAsset(getAssets(),"Calculator.ttf");
    // get just buttons ids
}

If I use findViewById I will get the id for a single button , I want a fast way to get all button ids and then change their font. ex I have : button1, button_a, buttonout ...

Mike
  • 4,550
  • 4
  • 33
  • 47
Mickeljh
  • 113
  • 1
  • 1
  • 7
  • if i have like 10 buttons then i will put findViewById for each single button, i want a fast way to do that – Mickeljh Dec 08 '15 at 18:14

4 Answers4

0

It may help you!

    Button button= (Button) findViewById(R.id.button1);
    Typeface face8 = Typeface.createFromAsset(getApplicationContext().getAssets(), "MyFont.ttf");
    button.setTypeface(face8);
Sathish Kumar
  • 919
  • 1
  • 13
  • 27
0

This question: Set font for all textViews in activity? shows how to achieve this for TextViews.

If you want to do the same thing for buttons, use this code:

private void overrideFonts(final Context context, final View v) {
try {
    if (v instanceof ViewGroup) {
        ViewGroup vg = (ViewGroup) v;
        for (int i = 0; i < vg.getChildCount(); i++) {
            View child = vg.getChildAt(i);
            overrideFonts(context, child);
     }
    } else if (v instanceof Button ) {
        ((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
    }
} catch (Exception e) {
}
}

All credits go to Agarwal Shankar for this code

As for the parameters: for context you probably want to use this and for v you want to use the parent View, which is typically the upper Layout attribute (LinearLayout or whatever you're using)

Community
  • 1
  • 1
Xander
  • 5,487
  • 14
  • 49
  • 77
  • this maybe will works , but i want to take the Viewgroup on onCreate() method not on a method that i need to create it , can i do that ? – Mickeljh Dec 08 '15 at 18:36
  • Sure! Just paste the above method anywhere in the Activity class and then call this method from `onCreate()`. – Xander Dec 08 '15 at 18:50
0

First get all buttons in your layout, then change the typeface of everyone, let's say that viewGroup is your root View (could be RelativeLayout, LinearLayout, etc...).

int count = viewGroup.getChildCount();
      for (int i = 0; i< count; i++){
          if(viewGroup.getChildAt(i) instanceof Button){
              //do whatever you want here
          }
      }

you can get the root View by giving it an if in the xml file.

whd.nsr
  • 684
  • 6
  • 12
0

you can make a a simple method like this

private void typefaces(int... args){
    Typeface buttontypeface = Typeface.createFromAsset(getAssets(), "Calculator.ttf");
    for(int i = 0; i < args.length; i++){
        ((Button) findViewById(args[i])).setTypeface(buttontypeface);
    }
}

and then call it in onCreate

typefaces(R.id.button, R.id.button1, R.id.button2, R.id.button3);

where R.id.button, R.id.button1, R.id.button2, R.id.button3 are the ids from 4 Buttons. You can add as many as you want

Vasileios Pallas
  • 4,801
  • 4
  • 33
  • 50