Setting Action Bar Title and Subtitle

Setting Action Bar title and subtitle

You can do something like this to code for both versions:

/**
* Sets the Action Bar for new Android versions.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void actionBarSetup() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");
}
}

Then call actionBarSetup() in onCreate(). The if runs the code only on new Android versions and the @TargetApi allows the code to compile. Therefore it makes it safe for both old and new API versions.

Alternatively, you can also use ActionBarSherlock (see edit) so you can have the ActionBar on all versions. You will have to do some changes such as making your Activities extend SherlockActivity and calling getSupportActionBar(), however, it is a very good global solution.

Edit

Note that when this answer was originally written, ActionBarSherlock, which has since been deprecated, was the go-to compatibility solution.

Nowadays, Google's appcompat-v7 library provides the same functionality but is supported (and actively updated) by Google. Activities wanting to implement an ActionBar must:

  • extend AppCompatActivity
  • use a Theme.AppCompat derivative

To get an ActionBar instance using this library, the aptly-named getSupportActionBar() method is used.

Setting SupportActionBar subtitle multiline

Unfortunately, maxLines is not a part of TextAppearance so changing it will not affect the subtitle appearance. Moreover, there's no legal way to access Toolbar.mSubtitleTextView, but after setting a subtitle text you can access it via reflection and change its appearance as you wish.

UPDATE:

That's how you can access mSubtitleTextView via reflection.

public class SubtitleAccessor {
private static final Field sSubtitleTextViewField = getSubtitleTextViewField();

private static Field getSubtitleTextViewField() {
try {
Field field = Toolbar.class.getDeclaredField("mSubtitleTextView");
field.setAccessible(true);
return field;
} catch (NoSuchFieldException exception) {
return null;
}
}

public static TextView getSubtitleTextView(Toolbar toolbar) {
if (sSubtitleTextViewField == null) {
return null;
}

try {
return (TextView) sSubtitleTextViewField.get(toolbar);
} catch (IllegalAccessException exception) {
return null;
}
}
}

How to add Textview below Actionbar title

You can use setSubtitle .

Set the action bar's subtitle. This will only be displayed if
DISPLAY_SHOW_TITLE is set. Set to null to disable the subtitle
entirely.

ActionBar ab = getActionBar(); //getSupportActionBar()
ab.setTitle("App Title");
ab.setSubtitle("App sub-title");

You can check Setting Action Bar title and subtitle

Setting a subtitle on my Toolbar from a fragment

To use the Toolbar and the Appcompat 21, you have to use an AppCompatActivity and use:

((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle("About");

How to customize the Action Bar subtitle Font?

If you don't already have this namespace attribute in your layout file add it:

xmlns:app="http://schemas.android.com/apk/res-auto"

Update your Toolbar attributes in your layout file like this:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextAppearance="@style/ToolbarTitleAppearance"
app:subtitleTextAppearance="@style/ToolbarSubtitleAppearance"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>

Add styles like these:

<style name="ToolbarTitleAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">20dp</item>
<!-- include other attributes you want to change: textColor, textStyle, etc -->
</style>

<style name="ToolbarSubtitleAppearance" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
<item name="android:textSize">14dp</item>
</style>

Comments on this answer discuss whether the units for textSize should be dp or sp. When I originally wrote this answer, I looked at the Android source files and saw that dp was used. I recommend staying with that. As FrancescoDonzello explains in his comment, the size of the Toolbar is fixed and, unlike other widgets, will not expand to contain text enlarged by changes to the phone settings.

Change Android Actionbar subtitle colour

Just change titleTextStyle to subtitleTextStyle

<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
</style>

<style name="MyTheme.ActionBarStyle"parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:subtitleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
</style>

<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">@color/red</item>
</style>

How to change the fonts of Title in Action bar in android

Just use these code:

if you are using "Holo Theme" then use this:

    int titleId = getResources().getIdentifier("action_bar_title", "id",
"android");
TextView yourTextView = (TextView) findViewById(titleId);
yourTextView.setTextColor(getResources().getColor(R.color.black));
Typeface yourTypeface = Typeface.createFromAsset(getAssets(), "fonts/your_font.ttf");
//or get from resource
yourTextView.setTypeface(yourTypeface);

if you are using "Material Theme" then use this:

Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar);
TextView textView1 = (TextView) toolbar.getChildAt(0);//title
TextView textView2 = (TextView) toolbar.getChildAt(1);//subtitle
textView1.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
textView2.setTextColor(getResources().getColor(R.color.colorAccent));
Typeface yourTypeface1 = Typeface.createFromAsset(getAssets(), "fonts/your_font1.ttf");
Typeface yourTypeface2 = Typeface.createFromAsset(getAssets(), "fonts/your_font2.ttf");
//or get from resource
textView1.setTypeface(yourTypeface1);
textView2.setTypeface(yourTypeface2);

it works and it is very simple!
Sample Image



Related Topics



Leave a reply



Submit