4

I'm using navigation component and material toolbar. I've just setup navigation component with material toolbar. By doing so whenever fragment changed the back button shown and the title of material toolbar changed by navigation component automatically.

The question is I just want to change the title gravity into center without removing those navigation components toolbar support.

How can I do that ?

Note : I`ve tried to change the toolbar style, but it seems does not work. And creating an extra text view into toolbar view its not a solution since I want to use navigation component toolbar support.

Thanks in advance.

Mehdi Jahed Manesh
  • 2,228
  • 2
  • 17
  • 34

3 Answers3

12

Starting from com.google.android.material:material:1.4.0 you can use app:titleCentered="true" to center the title.

<com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/materialToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:title="Toolbar"
            app:titleCentered="true"/>

I hope this helps. You can mark it as accepted if it matches your needs.

Mbuodile Obiosio
  • 1,463
  • 1
  • 15
  • 19
0

You can use a Custom Toolbar

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/action_bar_bkgnd"
app:theme="@style/ToolBarTheme" >


 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toolbar Title"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title" />


<androidx.appcompat.widget.Toolbar/>

It's just a part of view Group, You can style the TextView however you would like because it's just a regular TextView. So in your activity you can access the title like so:

Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);
0

As a tricky way you can use: com.google.android.material.appbar.CollapsingToolbarLayout

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:collapsedTitleGravity="center"
            app:collapsedTitleTextAppearance="?attr/textAppearanceHeadline3"
            app:expandedTitleGravity="center"
            app:expandedTitleTextAppearance="?attr/textAppearanceHeadline1"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:contentInsetStart="0dp"
                app:contentInsetStartWithNavigation="0dp"
                app:layout_collapseMode="pin"
                app:navigationIcon="@drawable/ic_back_24dp"
                app:title="@string/parvaneh_personal_info" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>
hamid Mahmoodi
  • 690
  • 4
  • 16