Android examples for User Interface:ActionBar Title
Sets up the title, appearance, and behavior of the ActionBar if it is supported.
/* ActivityHelper.java//from www .j a v a 2s.c om * See the file "LICENSE.md" for the full license governing this code. */ import android.annotation.TargetApi; import android.app.ActionBar; import android.app.Activity; import android.content.res.Resources; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Build; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; public class Main{ /** * Sets up the title, appearance, and behavior of the ActionBar if it is * supported. Should be invoked after * {@link #addActionBarIfSupported(Activity)}. * * @param activity * @param titleTextView * @param title * @param titleTextViewPrefix * @param icon */ public static void setupActionBarIfSupported(Activity activity, TextView titleTextView, String title, String titleTextViewPrefix, Drawable icon) { if (titleTextViewPrefix == null) { titleTextViewPrefix = ""; } activity.setTitle(titleTextViewPrefix + title); if (actionBarSupported() && actionBarNonNull(activity)) { setupActionBar(activity, title, icon); if (titleTextView != null) { titleTextView.setVisibility(View.GONE); } } else { if (titleTextView != null) { titleTextView.setText(title); } } } private static boolean actionBarSupported() { return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB; } @TargetApi(Build.VERSION_CODES.HONEYCOMB) private static boolean actionBarNonNull(Activity activity) { return activity.getActionBar() != null; } @TargetApi(Build.VERSION_CODES.HONEYCOMB) private static void setupActionBar(Activity activity, String title, Drawable icon) { ActionBar bar = activity.getActionBar(); bar.setBackgroundDrawable(Appearance.ACTIONBAR_BACKGROUND); bar.setTitle(title); int actionBarTitleId = Resources.getSystem().getIdentifier( "action_bar_title", "id", "android"); TextView actionBarTextView = (TextView) activity .findViewById(actionBarTitleId); if (actionBarTextView != null) { actionBarTextView.setTextColor(Color.WHITE); } bar.setDisplayHomeAsUpEnabled(false); if (icon != null && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { setActionBarHomeIcon(bar, icon); } else { bar.setDisplayShowHomeEnabled(false); } } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private static void setActionBarHomeIcon(ActionBar bar, Drawable icon) { bar.setIcon(icon); } }