change Title Fonts for ActionBar - Android User Interface

Android examples for User Interface:ActionBar Title

Description

change Title Fonts for ActionBar

Demo Code


import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Build;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
import android.util.Log;

public class Main{
    private static final String TAG = "ABHException";
    //from   w  w  w  . java2s.c o m
    public static void changeTitleFonts(Activity activity, Typeface typeface) {
        if (typeface == null || activity == null) {
            Log.e(TAG, "activity? typeface???!");
            return;
        }
        if (activity instanceof ActionBarActivity) {
            try {
                android.support.v7.app.ActionBar actionBar = ((ActionBarActivity) activity)
                        .getSupportActionBar();
                if (actionBar != null)
                    setTitle(actionBar, typeface, actionBar.getTitle()
                            .toString());
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }

        } else if (activity instanceof AppCompatActivity) {
            try {
                android.support.v7.app.ActionBar actionBar = ((AppCompatActivity) activity)
                        .getSupportActionBar();
                if (actionBar != null)
                    setTitle(actionBar, typeface, actionBar.getTitle()
                            .toString());
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }
        } else {
            try {
                ActionBar actionBar = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    actionBar = activity.getActionBar();
                    if (actionBar != null)
                        setTitle(actionBar, typeface, actionBar.getTitle()
                                .toString());
                }
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }
        }
    }
    
    public static void setTitle(Activity activity, Typeface typeface,
            String title) {
        if (activity instanceof ActionBarActivity) {
            try {
                android.support.v7.app.ActionBar actionBar = ((ActionBarActivity) activity)
                        .getSupportActionBar();
                setTitle(actionBar, typeface, title);
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }

        } else if (activity instanceof Activity) {
            try {
                ActionBar actionBar = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    actionBar = activity.getActionBar();
                }
                setTitle(actionBar, typeface, title);
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }
        }
    }
    
    public static void setTitle(android.support.v7.app.ActionBar actionBar,
            Typeface typeface, String title) {
        if (typeface == null || actionBar == null) {
            Log.e(TAG, "typeface?actionbar??");
            return;
        }
        SpannableString sp = new SpannableString(title);
        sp.setSpan(new TypefaceSpan(typeface), 0, sp.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        setTitle(actionBar, sp);
    }
    
    public static void setTitle(ActionBar actionBar, Typeface typeface,
            String title) {
        if (typeface == null || actionBar == null) {
            Log.e(TAG, "typeface?actionbar??");
            return;
        }
        SpannableString sp = new SpannableString(title);
        sp.setSpan(new TypefaceSpan(typeface), 0, sp.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        setTitle(actionBar, sp);
    }
    
    public static void setTitle(android.support.v7.app.ActionBar actionBar,
            SpannableString spannableString) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN
                && Build.MANUFACTURER.toUpperCase().equals("LGE")) {
            actionBar.setTitle(spannableString.toString());
        } else {
            actionBar.setTitle(spannableString);
        }
    }
    
    public static void setTitle(ActionBar actionBar,
            SpannableString spannableString) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN
                && Build.MANUFACTURER.toUpperCase().equals("LGE")) {
            actionBar.setTitle(spannableString.toString());
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                actionBar.setTitle(spannableString);
            }
        }
    }
}

Related Tutorials