Andy
Andy

Reputation: 518

How to change the Typeface of the Title/Subtitle of an Actionbar?

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?

Upvotes: 0

Views: 302

Answers (2)

Andy
Andy

Reputation: 518

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;
}

Upvotes: 0

Swayam
Swayam

Reputation: 16364

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);

Upvotes: 3

Related Questions