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
?
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>
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With