Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can put a text at the center in CollapsingToolbarLayout and Toolbar?

I have a title on the left side in a Toolbar and in a CollapsingToolbarLayout. I want to move it to the center horizontally. Please help me.

Pictures of my current layout and what I want to achieve:

Current layout 1 Current layout 2

Code:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sagar.materialdesigndemo.CollapsingToolbarFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    app: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:elevation="@dimen/toolbar_elevation"
        app:expandedTitleTextAppearance="@style/HeaderTitleStyle"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/flexible_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/backmain"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:titleTextAppearance="@style/ToolbarTitleStyle"
            android:layoutDirection="rtl"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" />

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

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

like image 705
Mahdi Mortazavi Avatar asked Dec 19 '22 21:12

Mahdi Mortazavi


2 Answers

Set this parameter in your CollapsingToolbarLayout

app:expandedTitleGravity="center_horizontal"
like image 123
Context Avatar answered Apr 07 '23 19:04

Context


To achieve this, you can get reference to the collapsing toolbar from the corresponding Java class and set the gravity of your title:

CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);

//now that you have access to your toolbar, set the directions.
collapsingToolbar.setExpandedTitleGravity(Gravity.CENTER_HORIZONTAL);
collapsingToolbar.setCollapsedTitleGravity(Gravity.CENTER_HORIZONTAL);
like image 44
superus8r Avatar answered Apr 07 '23 18:04

superus8r