This is the code to use a custom text in actionbar:
ActionBar act = ((ActionBarActivity) getActivity()).getSupportActionBar();
LayoutInflater inflator = LayoutInflater.from(getActivity());
View v = inflator.inflate(R.layout.action_bar_custom, null);
((MyCustomTextView) v.findViewById(R.id.title_text)).setText(title);
act.setCustomView(v);
In this case we have a custom TextView
into a normal layout we inflate to set as a view in the ActionBar
, the layout will look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ch.nuewsli.widget.TestoTextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/WHITE_TEXTCOLOR"
android:textSize="18sp"
android:lines="1"
android:gravity="center_vertical"/>
</LinearLayout>
Since it is not possible to change the ActionBar
font from the xml (but only the font family) you have to extend a TextView
, force it to use your font and put it into your ActionBar
, remember to copy your font into the assets
folder.
The code below is capable to distinguish the style of your TextView
specified by the android:textStyle
attribute between bold
and the others.
UPDATE
Just put this code of your TextView
:
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
this(context, attrs);
}
public MyTextView(Context context) {
super(context);
this.mContext = context;
init();
}
public void init() {
Typeface typeface = getTypeface();
if(typeface != null)
style = typeface.getStyle();
if(style != Typeface.BOLD) {
typeface = Typeface.createFromAsset(mContext.getAssets(), "My-Font-Regular.ttf");
}else {
typeface = Typeface.createFromAsset(mContext.getAssets(), "My-Font-Bold.ttf");
}
this.setTypeface(typeface);
}