Search code examples
androidandroid-toolbarandroid-collapsingtoolbarlayout

Tile not getting center in Collapsed toolbar


I tried setting collapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);

collapsingToolbarLayout.setExpandedTitleGravity(Gravity.CENTER);

and almost all available links on INTERNET but i am not able to put the title in center of toolbar.

enter image description here

plz help !


Solution

  • make your Toolbar like this

           <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:contentInsetEnd="0dp"
                app:contentInsetLeft="0dp"
                app:contentInsetRight="0dp"
                app:contentInsetStart="0dp"
                app:contentInsetStartWithNavigation="0dp"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:text="Center">
    
                    <TextView
                        android:layout_marginLeft="-25dp"
                        android:id="@+id/toolbar_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center" />
                </LinearLayout>
    
            </android.support.v7.widget.Toolbar>
    

    in your activity file change like this

    toolbar=(Toolbar)findViewById(R.id.tool_bar);
    TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    mTitle.setText("PREM");
    setSupportActionBar(toolbar);