Skip to content Skip to sidebar Skip to footer

Tile Not Getting Center In Collapsed Toolbar

I tried setting collapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER); collapsingToolbarLayout.setExpandedTitleGravity(Gravity.CENTER); and almost all available links o

Solution 1:

make your Toolbar like this

<android.support.v7.widget.Toolbarandroid: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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="vertical"android:text="Center"><TextViewandroid: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);

Solution 2:

To center the text in the toolbar you need to use a custom toolbar with a textview inside it rather than using android:title=""

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:id="@+id/toolbar"
    android:layout_height="?actionBarSize">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#000000"
        android:id="@+id/tvToolbarTitle"
        android:layout_gravity="center"
        android:textSize="16sp"/>

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

You can apply the same logic in a collapsible toolbar.

Post a Comment for "Tile Not Getting Center In Collapsed Toolbar"