14

I would like to apply custom font to the title of my app which is displayed on the ActionBar. Previously I didn't use any support library and this solution:

int titleId = getResources().getIdentifier("action_bar_title", "id",
                "android");
TextView yourTextView = (TextView) findViewById(titleId);
yourTextView.setTypeface(face);

worked fine for me. But now I am using material design SupportActionBar and this throws NullPointerException. So how to change the ActionBar font when using AppCompat?

juanjo
  • 87
  • 1
  • 8
fragon
  • 3,391
  • 10
  • 39
  • 76
  • 1
    This responce i think can you help http://stackoverflow.com/a/15181195/2778639 – Nickolay Savchenko Jan 03 '15 at 17:26
  • @user27799 Thanks, this post fixed my problem. If someone has similar problem to mine, just use the method from the linked post, which works fine with the support library v-21. – fragon Jan 04 '15 at 01:20
  • Here the solutions, [Actionbar custom design.][1] [1]: http://stackoverflow.com/questions/17966350/changing-the-font-of-the-application-title-in-android/21480484#21480484 – Karthikeyan Jul 24 '15 at 10:39

3 Answers3

6

Assuming you are already successfully using the new toolbar of AppCompat v22+ and that you are importing android.support.v7.widget.Toolbar in your activity.

So on your OnCreate you should already have

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

at this point you are almost done:

import java.lang.reflect.field;

and add following lines:

TextView yourTextView = null;
try {
    Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
    f.setAccessible(true);
    yourTextView = (TextView) f.get(toolbar);
    yourTextView.setTypeface(face);
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
Daniele D.
  • 2,624
  • 3
  • 36
  • 42
2

Use Toolbar instead of ActionBar.follow below steps to add toolbar: 1.change your app theme to Theme.AppCompat.Light.NoActionBar 2.Goto project structure from File menu,goto library dependencies,add design library and sync your project. 3.Goto your layout where you want to display ActionBar and write below code:

<android.support.v7.widget.Toolbar
    android:id="@+id/mytoolbar"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/pccolor"
    android:title="Services"
    app:titleTextAppearance="@style/Toolbar.TitleText"
    android:elevation="4dp">
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toolbar Title"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>

4.In the Activity,write as below:

 Toolbar mytoolbar=(Toolbar)findViewById(R.id.mytoolbar);
    setSupportActionBar(mytoolbar);

To customize your title font:

fontPath = "Fonts/Roboto-Light.ttf";
    Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), fontPath);

TextView tv_ins=(TextView)FindViewById(R.id.toolbar_title);
 tv_ins.setText("mytitle");
tv_ins.setTypeface(tf);

Try this out Hopes this helps you,if anything wrong please let me know.

Rushi Ayyappa
  • 2,708
  • 2
  • 16
  • 32
-2

prepare Custom Layout ,put text view in action_bar_layout and then call actionbar.setcustomlayout()

Zoom Azure
  • 41
  • 3