Android Multiline Toolbar Title
Asked Answered
A

2

10

I have a Toolbar that when in landscape mode isn't as wide as usual, but it has more height than usual and for that reason I want to set the title to be multiline (2 lines to be precise) when it is in landscape. I have tried some things where I put a TextView inside of the Toolbar, but when I try to access the TextView to set it's title (since the the title is variable) I get a NullPointer after using findViewById.

Here is some code of the current situation (without multiline):

@Override
protected void onResume() {
    super.onResume();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = (int)( size.x * 0.37);
        int height =  Math.round(size.x * 0.63f * 0.25f);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
        toolbar.setLayoutParams(params);
    }
    if (executeOnResume) {
        currentProject = getIntent().getParcelableExtra("Project");
        getSupportActionBar().setTitle("This is a pretty long piece of text, I hope it fits");
        // A lot of irrelevant code...
        //...
        //...
    } else { loadStuff();}
}

And here is the toolbar xml:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:title="@string/menuLabel1a"
        app:layout_widthPercent="37%"
        app:layout_heightPercent="26%"
        android:gravity="top"
        app:titleTextAppearance="@style/toolbarTitleText"
        android:background="@color/greyLight"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
Alidaalidade answered 27/5, 2016 at 8:52 Comment(0)
T
17

Try this :

 <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar2"
    android:layout_width="match_parent"
    android:layout_height="@dimen/double_height_toolbar"
    android:gravity="top"
    app:titleTextAppearance="@style/toolbarTitleText"
    android:background="@color/greyLight"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/product_details_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:title="@string/menuLabel1a"
            android:paddingTop="@dimen/padding_normal"
            style="@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse"
            android:maxLines="2" />

</android.support.v7.widget.Toolbar>

Other Solution is for ActionBar

The default TextView of the ActionBar does not support line wrapping, and there is no exposed method to set it to wrap. So you can 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"
    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.");
Towhee answered 27/5, 2016 at 8:59 Comment(6)
I don't really have an explicit subtitle, but just a title that is very long. Setting a subtitle would involve using 2 different strings (1 for the title, 1 for the subtitle), but I only have one String, so I don't see how this really helps.Alidaalidade
If I would use the updated answer, would I set the title the same way or would I have to do it in a different way?Alidaalidade
@DennisvanOpstal remove this line in your toolbar android:title="@string/menuLabel1a" and add it in TextView. it solve your problem.Towhee
That is just a tempory title, I set the title programmatically in the onResume in my Activity class like getSupportActionBar().setTitle("This is a pretty long piece of text, I hope it fits"); .Alidaalidade
@DennisvanOpstal I have posted other solution too try it.Towhee
@DennisvanOpstal glad to help you.Towhee
C
1

Found this solution who was work for me :

fun Toolbar.disableTitleSingleLine() {
    for (i in 0..childCount) {
        val child = getChildAt(i)
        if (child is TextView) {
            child.isSingleLine = false
        }
    }
}

According Toolbar implementation, the title TextView is created when text been set. It's important to call this extension after title set.

Cylindrical answered 13/12, 2021 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.