Is it possible to create the title in the Action Bar with more than one line? I need a double spaced title.
4 Answers
I am not quite sure if you were looking for the following, but:
actionBar.setSubtitle(String string) sets a second line with smaller font underneath your main Title.
example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left_drawer_item_clicked);
String value = getIntent().getExtras().getString("drawerItemString");
ActionBar actionBar = getActionBar();
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
actionBar.setTitle(selectedBook.getTitle());
actionBar.setSubtitle(selectedBook.getAuthorName());
}

- 391
- 3
- 3
-
1Best solution listed here. Should be the marked as the answer! – Johan May 13 '14 at 18:27
-
The issue with this solution, is that if authorName is empty, the title will still show up on the first line, while the second line remains empty. That look is not good... – Andrei F Oct 08 '14 at 13:30
-
@AndreiF If this is the case, you just have to put a condition that evaluates if authorName is empty. If empty, setSubtitle is not executed. – cesargastonec Apr 26 '21 at 17:19
I found a better solution that works perfectly up to Android 4.2.2 where the icon and the title are one clickable item.
int titleId = Resources.getSystem()
.getIdentifier("action_bar_title", "id", "android");
if (titleId > 0) {
TextView title = (TextView) findViewById(titleId);
title.setSingleLine(false);
title.setMaxLines(2);
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
}
If you are using ActionBarSherlock, use this code:
int titleId = Resources.getSystem()
.getIdentifier("action_bar_title", "id", "android");
if (titleId == 0) {
titleId = com.actionbarsherlock.R.id.abs__action_bar_title;
}
TextView title = (TextView) findViewById(titleId);
if (title != null) {
title.setSingleLine(false);
title.setMaxLines(2);
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
}

- 15,689
- 6
- 63
- 73
-
This works up through 4.4 using support library's ActionBar as well. But fails on platforms <= 2.3 – NKijak Feb 28 '14 at 16:40
The default TextView on the ActionBar does not support line wrapping, and there is no exposed method to set it to wrap. So create a flexible layout, like this: action_bar_title_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/action_bar_title"
style="@style/TextAppearance.AppCompat.Title"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:ellipsize="end"
android:maxLines="2"/>
</LinearLayout>
Then set this as the custom layout on your ActionBar:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.action_bar_title_layout);
((TextView) findViewById(R.id.action_bar_title)).setText(
"This is a long text title that will wrap to multiple lines, when necessary.");

- 7,051
- 1
- 53
- 59
No, but you can replace the icon with a separate logo graphic (android:logo
and then setDisplayUseLogoEnabled()
). You could embed your two-line title as part of the logo graphic and then not show the original title string via setDisplayShowTitleEnabled()
. AFAIK, that combination should work.

- 986,068
- 189
- 2,389
- 2,491
-
Thanks for the hint, but I solved it with [setCustomView](http://developer.android.com/reference/android/app/ActionBar.html#setCustomView(int)) – benkap Jul 01 '12 at 20:19