I added a Toolbar
in my app.
For that I am using the below xml code for custom toolbar layout.
Toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#212E42"
android:minHeight="?attr/actionBarSize">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/lefttoolbarContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/righttoolbarContainer"
android:background="#212E42">
<TextView
android:id="@+id/txtActionBarTitle"
style="@style/TextViewLargeWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:padding="@dimen/fivedp"
android:text="demo title for toolbar"
android:visibility="visible" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/righttoolbarContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<ImageView
android:id="@+id/ivActionBarRightTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/fivedp"
android:contentDescription="@string/empty_description"
android:padding="@dimen/fivedp"
android:src="@drawable/ic_chart"
android:visibility="visible" />
<ImageView
android:id="@+id/ivActionBarRightOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/threedp"
android:layout_toRightOf="@+id/ivActionBarRightTwo"
android:contentDescription="@string/empty_description"
android:padding="@dimen/fivedp"
android:src="@drawable/ic_settings"
android:visibility="gone" />
</RelativeLayout>
</RelativeLayout>
In Android Studio output, the toolbar title is displayed in centre, as shown below.
But when I run my app, I can't get toolbar title at centre, as shown below :
This issue raises when I add the Toolbar navigation icon dynamically in my app.
After adding the navigation icon, the icon gets its space and the toolbar title goes to the right side.
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setNavigationIcon(R.drawable.logo)//Toolbar navigation icon will add here
setSupportActionBar(mToolbar);
EDIT:
My Toolbar
which I want to achieve is below ,For that please check updated Toolbat.xml:
Please, help me to get the Toolbar title centered!
You can remove nested relativelayout and add an imageview to the relativelayout
lefttoolbarContainer
. Since your title is inParentCenter, it shouldn't move to the right when you add the image in code(atleast in theory). i.eToolbar.xml
And in your activity/fragment
Hope it helps..
Try changing the textview attribute
with this: