2

I am trying to center my ToolBar title:

<android.support.design.widget.AppBarLayout
    android:id="@+id/top_app_bar_layout"
    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:theme="@style/ThemeToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="LOGO"
            android:textColor="@color/colorAccent"
            android:textSize="32sp"
            android:textStyle="bold"
            tools:ignore="HardcodedText" />

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

ToolBar aligns it's title left by default. To align the title center I have added a TextView inside the tool bar. But it doesn't center exactly. I have try to give negative margin from left but it didn't work.

enter image description here How can I do it?

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112

2 Answers2

8

I had a similar problem, can't remember where I found the answer but Toolbar has a 16dp content inset by default. You need to remove that to center the logo.

app:contentInsetStart="0dp"

Make sure you use the app name space.

So your Toolbar layout would look like this:

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:paddingLeft="0dp"
            android:paddingRight="0dp"
            android:layout_width="match_parent"
            app:contentInsetStart="0dp"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary">

            <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="LOGO"
                    android:textColor="@color/colorAccent"
                    android:textSize="32sp"
                    android:textStyle="bold"
                    tools:ignore="HardcodedText" />

    </android.support.v7.widget.Toolbar>

UPDATE

Setting contentInsetStart to "0dp" also didn't work for me. So I tried giving negative padding (-48dp) to TextView and it worked.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:theme="@style/ThemeToolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="LOGO"
        android:textColor="@color/colorAccent"
        android:textSize="32sp"
        android:textStyle="bold"
        tools:ignore="HardcodedText"
        android:paddingStart="-48dp"
        android:paddingLeft="-48dp"/>

</android.support.v7.widget.Toolbar>
Darvex
  • 3,624
  • 2
  • 23
  • 39
Naveed
  • 2,942
  • 2
  • 25
  • 58
  • 1
    In an effort to keep the answer clear, I want to know why was this answer was edited to have negative padding. You would get weird results on different versions of android with different font if you use negative padding. The `contentInsetStart` works for me and I have found the original source from Chris Banes answer http://stackoverflow.com/a/26495926/2517849 – Naveed Nov 16 '15 at 16:45
  • This should work for all devices are you having a problem with a specific device? – Naveed Apr 28 '19 at 17:00
1

try to wrap_content on the textview width and set layout_gravity to center. If it's not centred well, then the negative margin wild work.

Laurent Russier
  • 658
  • 5
  • 24