2

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. enter image description here

But when I run my app, I can't get toolbar title at centre, as shown below : enter image description here

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:

enter image description here

Please, help me to get the Toolbar title centered!

pRaNaY
  • 24,642
  • 24
  • 96
  • 146

2 Answers2

0

Try changing the textview attribute

android:gravity="center"

with this:

 android:layout_gravity="center"
0

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.e

Toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/default_dark_blue"
android:minHeight="?attr/actionBarSize">

<RelativeLayout
    android:id="@+id/lefttoolbarContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#212E42">

    <ImageView
        android:id="@+id/navigationIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="56dp"
        android:scaleType="center"
        android:layout_alignParentLeft="true"/>

    <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>
</android.support.v7.widget.Toolbar>

And in your activity/fragment

mToolbar = (Toolbar) findViewById(R.id.toolbar);
ImageView navigationIcon = mToolbar.findViewById(R.id.navigationIcon);
navigationIcon.setImageResource(R.drawable.logo);
setSupportActionBar(mToolbar);

Hope it helps..

Fabin Paul
  • 1,701
  • 1
  • 16
  • 18