Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center title in AppBarLayout with MaterialToolbar

I try to center the text in my toolbar. I use AppBarLayout and MaterialToolbar, as suggested in Material Design.

I tried everything I found on StackOverflow to center the title, but nothing seemed to work.

This is my toolbar:

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/topAppBar"
        style="@style/CenteredToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

</com.google.android.material.appbar.AppBarLayout>

And CenteredToolbar is my custom style where I tried to center the title:

<style name="CenteredToolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
    <item name="android:gravity">center_horizontal</item>
</style>

This doesn't work, however.

Any suggestions on what I can do to center the title?

like image 464
Terry Avatar asked Oct 19 '25 03:10

Terry


1 Answers

I shared my answer for this here. From com.google.android.material:material:1.4.0 there's app:titleCentered="true"

https://stackoverflow.com/a/69589951/2810726

like image 89
Mbuodile Obiosio Avatar answered Oct 20 '25 18:10

Mbuodile Obiosio