0

I want to customize the typeface of the title and subtitle of my ActionBar. For this I placed the desired TTF-file in the asset folder and loaded the typeface by saying
Typeface font = Typeface.createFromAsset(getAssets(), "ARDESTINE.TTF");
It's easy to apply this typeface to views like Button or TextView etc., but how can I apply it to the ActionBar, I cannot find any setter-method? Or do I have to use getActionBar().setCustomView(view)
in order to achieve this?

Andy
  • 488
  • 4
  • 13
  • 1
    Your question is similar to this: http://stackoverflow.com/questions/8607707/how-to-set-a-custom-font-in-the-actionbar-title – 4tee Mar 21 '15 at 14:07

2 Answers2

3

You can create a SpannableString in your desired Typeface and set that string as the title of the ActionBar.

String name = "....."; // your string here
SpannableString s = new SpannableString(name);
s.setSpan(new TypefaceSpan(getApplicationContext(),BOLD_FONT_FILENAME, getResources().getColor(R.color.white), getResources().getDimension(R.dimen.size16)), 0, s.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actionBar.setTitle(s);
Swayam
  • 16,294
  • 14
  • 64
  • 102
0

I also found an alternative to the above solution. Put this in your on CreateOptionsMenu() method:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    this.setTitle(R.string.app_name_short);
    getActionBar().setSubtitle(R.string.app_name_full);
    int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
    int subtitleId = getResources().getIdentifier("action_bar_subtitle", "id", "android");
    Typeface font = Typeface.createFromAsset(getAssets(), "ARDESTINE.TTF");
    ((TextView) findViewById(titleId)).setTypeface(font);
    ((TextView) findViewById(subtitleId)).setTypeface(font);
    // getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.checker));
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
Andy
  • 488
  • 4
  • 13