Reputation: 10891
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
?
Upvotes: 10
Views: 10175
Reputation: 173
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);
}
Upvotes: 3
Reputation: 1121
Use getSupportActionBar().setTitle(getString(R.string.title));
within the activity. This can be done in onCreate
should be done whenever the locale is changed
Upvotes: 4
Reputation: 10891
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"
Upvotes: 2
Reputation: 9150
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>
Upvotes: 13