Ortiz
Ortiz

Reputation: 294

How can I set a custom font for the action bar text, and make it happen from startup?

I can't get it done. My App starts with the standard font, the custom activity layout is inflated, and just then my font changes. As far as I know, I cannot directly code a custom font into the xml file of the layout I intend to inflate onto the action bar, so I got a code from another topic on the net about doing the change by referencing the TextView in the action bar and setting it in the class. Is there actually a way to make the font "standard", so that the app will already startup showing my custom font?

Here is the relevant code for the main activity, which I'm trying to change:

    public class Amd extends Activity {

    DatabaseHandler dh = new DatabaseHandler(this);
    public ProgressDialog mProgressDialog;
    public JSONObject jsonOb;
    public Filme m;
    public ArrayList<Genero> g;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.amd);
        setActionBar();
    }

    @SuppressLint("InflateParams") private void setActionBar(){
        this.getActionBar().setDisplayShowCustomEnabled(true);

        LayoutInflater inflator = LayoutInflater.from(this);
        View v = inflator.inflate(R.layout.custom_action_bar, null);

        Typeface font = Typeface.createFromAsset(this.getAssets(), "tcb.ttf");
        TextView textoActionBar = (TextView) getWindow().findViewById(Resources.getSystem().getIdentifier("action_bar_title", "id", "android"));
        textoActionBar.setTypeface(font);
        textoActionBar.setText("TESTE");
        this.getActionBar().setCustomView(v);
    }

Here is the styles xml, which I am also using to theme and color the action bar:

    <resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
        <item name="android:actionBarStyle">@style/PurpleActionBar</item>
    </style>

    <style name="PurpleActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">#5D4480</item>
        <item name="android:titleTextStyle">@style/ActionText</item>
    </style>

    <style name="ActionText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#975CE6</item>
    </style>

</resources>

Here is the manifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.virosys.amd"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
        <activity
            android:name="com.virosys.amd.activities.Amd"
            android:label="AMD"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Here is the layout I'm inflating to the main activity:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/Purple"
    tools:context="com.virosys.amd.activities.Amd" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="4dp"
            android:layout_marginRight="2dp"
            android:layout_marginBottom="2dp"
            android:onClick="listasOuFilmes"
            android:scaleType="fitXY"
            android:background="@null"
            android:src="@drawable/filmes" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginRight="4dp"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="2dp"
            android:layout_marginBottom="2dp"
            android:onClick="buscarFilme"
            android:scaleType="fitXY"
            android:background="@null"
            android:src="@drawable/busca" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/ImageButton3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginLeft="4dp"
            android:layout_marginBottom="4dp"
            android:layout_marginRight="2dp"
            android:layout_marginTop="2dp"
            android:onClick="inserirFilme"
            android:scaleType="fitXY"
            android:background="@null"
            android:src="@drawable/noimage" />

        <ImageButton
            android:id="@+id/ImageButton4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="2dp"
            android:scaleType="fitXY"
            android:background="@null"
            android:src="@drawable/config" />
    </LinearLayout>

</LinearLayout>

What else should I need to specify, or do you need any other code to understand what I'm trying to do?

Upvotes: 1

Views: 1552

Answers (2)

romtsn
romtsn

Reputation: 12002

Since Android ActionBar setTitle method receives a CharSequence as a parameter you can simply replace the call of your setActionBar() method with the next lines, that using span:

Typeface font = Typeface.createFromAsset(this.getAssets(), "tcb.ttf");
String actionBarTitle = "TESTE";
SpannableStringBuilder ssb = new SpannableStringBuilder(actionBarTitle);
ssb.setSpan(new CustomTypefaceSpan("", font), 0, actionBarTitle.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
getActionBar().setTitle(ssb);

And don't forget to add this custom typeface span class into your project.

public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}

Upvotes: 3

eclipse1203
eclipse1203

Reputation: 635

What you want to do is create a custom class that extends TextView, then within the constructor set your default font. Then reference that custom textview in your xml instead of Androids TextView.

Upvotes: 0

Related Questions