Search code examples
androidandroid-toolbar

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


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?


Solution

  • 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>