Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the font style on ActionBarSherlock

How do I change the font size for the title of the Action bar using Themes for ActionBarSherlock?

My theme at the moment (applied to my whole application is as follows):

<style name="Theme.MyAppDefaults" parent="@style/Theme.Sherlock.Light">
         <item name="android:textSize">@dimen/default_textsize</item>
         <item name="actionBarSize">@dimen/action_bar_height</item>
</style>

Notice that the font size is set as set on all the Views in my app. Just not the ActionBar's font.

like image 665
Diederik Avatar asked Jun 21 '12 10:06

Diederik


2 Answers

If you need to just change the title's style you can add a custom view with like this:

<TextView
    android:id="@+id/action_custom_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My Custom title"
    android:textColor="#fff"
    android:textSize="18sp" />

Then hide the actual title and show the custom view.

    _actionBar.setDisplayShowTitleEnabled(false);

     LayoutInflater inflater = LayoutInflater.from(this);
     View customView = inflater.inflate(R.layout.custom_action_layout, null);

     TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
     // you can apply a custom typeface here or do sth else...

     _actionBar.setCustomView(customView);
     _actionBar.setDisplayShowCustomEnabled(true);

That's all! If you are interested what is the default font size for ActionBar title it's 18sp.

like image 62
lomza Avatar answered Sep 20 '22 13:09

lomza


I got this working by using the below theme:

<style name="Theme.MyApp.Default" parent="@style/Theme.Sherlock.Light">
       <item name="actionBarStyle">@style/MyApp.SherlockActionBarStyle</item>
       <item name="android:actionBarStyle">@style/MyApp.SherlockActionBarStyle</item>
</style>

<style name="MyApp.SherlockActionBarStyle" parent="@style/Widget.Sherlock.ActionBar">
       <item name="titleTextStyle">@style/MyApp.ActionBar.TextAppearance</item>
</style>

<style name="MyApp.ActionBar.TextAppearance" parent="@style/TextAppearance.Sherlock.Widget.ActionBar.Menu">
      <item name="android:textSize">@dimen/default_textsize</item>
      <item name="android:textColor">@color/dark_grey</item>
 </style>

Notice the Text size is in the style named MyApp.ActionBar.TextAppearance. Also according to ActionBarSherlock the theme should be set on both android prefixed attributes and normal attributes (See actionBarStyle in style Theme.MyApp.Default above.)

like image 27
Diederik Avatar answered Sep 19 '22 13:09

Diederik