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?
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
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