Android examples for User Interface:ActionBar Title
set Title for ActionBar
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"; // ww w .ja va 2s . co m 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); } } } }