15

I want to load a fragment with the collapsed collapsingToolbar (i.e. in its non-expanded form). When I set the title of the collapsingToolbar without altering the appbarLayout layout parameters, the title is set properly and I can see the title.

The problem is, I also need to alter the appBarLayout layout parameters to prevent the collapsingToolbar from expanding (i.e. I want it to look and behave like a regular non-collapsing toolbar for this particular fragment). However, doing so makes the title no longer appear.

What I've tried: I've tried solutions listed at these pages to no avail:

MainActivity.java I believe I have isolated the problem to these lines, but I am not sure how to solve it.

collapsingToolbarLayout.setTitle("All Recent"); // works
appBarLayout.setExpanded(false, true); // works

// However, after adding the following lines, the above no longer works and the title does not appear in the toolbar
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)appBarLayout.getLayoutParams();
lp.height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, getResources().getDisplayMetrics());
appBarLayout.setLayoutParams(lp);

activity_main.xml

<!-- language: lang-xml -->

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        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/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:adjustViewBounds="true" />

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

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/frame_fragments"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floating_action_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"
        android:src="@mipmap/ic_add_a_photo"
        app:layout_anchor="@+id/appbar_layout"
        app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>

Title no longer shows after altering appbarLayout's parameters

Title no longer shows after altering appbarLayout's parameters

JMP
  • 4,417
  • 17
  • 30
  • 41
VIN
  • 6,385
  • 7
  • 38
  • 77

4 Answers4

53

I was able to solve this issue using the following answer provided by @DanielPersson at how to pin title in Toolbar inside CollapsingToolbarLayout:

collapsingToolbarLayout.setTitleEnabled(false);
toolbar.setTitle("My Title");

By calling setTitleEnabled(false);, the title appeared in the toolbar.

Community
  • 1
  • 1
VIN
  • 6,385
  • 7
  • 38
  • 77
  • 1
    It only works for me if I have the Toolbar.app:title set to a non empty value via xml first. After that this solution works... – JoachimR Jul 27 '17 at 07:49
4

Just add this line of code to the CollapsingToolbarLayout tag in your xml layout.

app:title="Title You Want"

It works If you want to Change the collapsingToolbarLayout title.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
3

It is the same as setting title in a normal toolbar.

In your xml layout file for collapsing toolbar, inside the CollapsingToolbarLayout you'll have a normal toolbar (android.support.v7.widget.Toolbar) for which you should set an id (android:id="@+id/toolbar"). Find this toolbar using the id in your java class and set the title.

  mToolbar = findViewById(R.id.toolbar);
  setSupportActionBar(mToolbar);
  getSupportActionBar().setTitle(R.string.home);
Apurva Thorat
  • 641
  • 5
  • 10
-7

Calling

this.setTitle("Whatever you want");

from onCreate of your activity should solve your problem.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135