1

I am using CollapsinToolbarLayout and I have included toolbar a TextView inside my Toolbar and want to make it centered.

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:fitsSystemWindows="true">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                android:layout_gravity="center"/>
        </android.support.v7.widget.Toolbar>

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

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

As can be seen above, I have set android:layout_gravity="center" and expecting to have my TextView centered inside my Toolbar

But, this is the result I get.

enter image description here

I am using View inside my other non CollapsingToolbarLayouts and they are showing according to the gravity I set, but I am not able to make it work with CollapsingToolbarLayout. So I suppose it has something to do with CollapsingToolbarLayout or its parent AppBarLayout but not able to figure out what.

Why I am getting this behavior when working with CollapsingToolbarLayout?

musooff
  • 6,412
  • 3
  • 36
  • 65
  • You can place a ConstraintLayout inside the Toolbar. In this ConstraintLayout, place the TextView as you need ( with the help of constraints). – Shubham Panchal Jun 07 '19 at 03:45

3 Answers3

1

Try this code. It will work!!!

<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginEnd="64dp"
    app:expandedTitleMarginStart="48dp"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        app:layout_collapseMode="parallax" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/image_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"
                android:contentDescription="@string/app_name"
                android:src="@drawable/back_icon" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Title"
                android:textColor="@color/white" />

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

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

Mohit patel
  • 296
  • 1
  • 8
0

try using gravity attribute like this android:gravity="center"

            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                android:gravity="center"
                android:layout_gravity="center"/>
        </android.support.v7.widget.Toolbar>
0

Some work around, try replacing the toolbar with below one

<android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title"
                    android:layout_marginRight="16dp"
                    android:layout_marginEnd="16dp"
                    android:gravity="center"/>
            </android.support.v7.widget.Toolbar>

Let me know if it worked how you want

MadLeo
  • 458
  • 1
  • 6
  • 24
  • I have to make it dynamic. Setting `android:layout_width="match_parent"` will not work when I have **back button** for example. – musooff Jun 07 '19 at 04:04