I currently have a navigation drawer with a toolbar that has a title, i wish to centre this title within the toolbar but the toolbar does not seem to take into consideration the drawer icon as you can see in the following picture.
whereas when i use the same toolbar layout for other activities that are not inside the navigation drawer the title is centred perfectly, as show in this picture:
So how do i get it to take into account this icon?
Here is my layout:
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/Theme.App.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/black"
app:popupTheme="@style/Theme.App.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@color/white"
android:textStyle="bold"
android:text="Title"
android:textSize="16sp" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
You need to understand that Toolbar widget extends ViewGroup class, and it has it's own LayoutParams.
So you do not require RelativeLayout inside Toolbar, and need to add just single line with TextView.
android:layout_gravity="center_horizontal"
And final xml should look like this,
<android.support.design.widget.AppBarLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.App.AppBarOverlay" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/black"
app:popupTheme="@style/Theme.App.PopupOverlay" >
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Title"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
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