Can't set title of toolbar that is inside an AppBarLayout
Asked Answered
P

4

10

I have a toolbar inside of an AppBarLayout like this:

<android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="right"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/editButton"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginRight="16dp"
                    android:scaleType="fitCenter"
                    android:background=
                        "?attr/selectableItemBackgroundBorderless"
                    android:contentDescription="Post"
                    android:src="@drawable/ic_mode_edit_24dp"
                    />

            </LinearLayout>

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

    </android.support.design.widget.AppBarLayout>

In my Activity.OnCreate() I try to set the title like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Edit");

(Not sure if this matters but..) After that I inflate a fragment:

MyFragment fragment = new MyFragment();
fragment.setArguments(savedInstanceState);

// Add fragment with tag
getSupportFragmentManager().beginTransaction()
    .add(R.id.content_frame, fragment, "my_frag")
    .commit();

However, this has no effect. In fact, there is no title at all. Is this because I have a LinearLayout inside of the Toolbar or something? Or because of the AppBarLayout?

Promptitude answered 23/3, 2016 at 19:18 Comment(2)
any reason you need to have a LinearLayout as a child of the Toolbar instead of using a menu?Amalamalbena
I would also like to know the same, I've been trying to keep it clean using labels from app activity to identify the purpose. I can imagine this being useful when fragments inside the activity change the purpose of the activity substantially?Boutin
D
13

you need to add a TextView inside the ToolBar

EDIT: something like this

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:contentInsetEnd="0dp"
        android:contentInsetLeft="0dp"
        android:contentInsetRight="0dp"
        android:contentInsetStart="0dp"
        android:elevation="2dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/beermap"
            android:textColor="@color/white"
            android:textSize="22sp"
            android:textStyle="bold" />

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

</android.support.design.widget.AppBarLayout>
Dossal answered 23/3, 2016 at 19:28 Comment(4)
Is there anything special about the TextView to add? I mean I want it to look like the default styled title for activities.Promptitude
no, is just a regular TextView.. then you can style it as you want, to make it look like a "regular" titleDossal
I'll accept this answer because it did fix my problem but a new problem is how to style that TextView to look just like the "regular" one!Promptitude
Still looks a bit off. I tried 20sp and that was closer, but can't get that font just right. Oh well Thanks.Promptitude
B
4

Use getSupportActionBar().setTitle(getString(R.string.title)); within the activity. This can be done in onCreate should be done whenever the locale is changed

Bagatelle answered 4/7, 2017 at 21:23 Comment(0)
J
3

If your toolbar is inside a collapsing toolbar layout

    public void setToolbarTitle(String s){
    Log.d(TAG, "setToolbarTitle: " + s);

    CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsingToolbar);
    collapsingToolbarLayout.setTitle(s);
}
Jordonjorey answered 13/6, 2018 at 2:2 Comment(0)
P
2

Here is how to make the textview look exactly like default:

<TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/beermap"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:fontFamily="sans-serif-medium" />

The key part being 20sp textsize and android:fontFamily="sans-serif-medium"

Promptitude answered 26/3, 2016 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.