Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CollapsingToolbarLayout.setTitle(title) does not work after changing appbarLayout

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:

  • CollapsingToolbarLayout setTitle() not working anymore
  • CollapsingToolbarLayout setTitle() does not update unless collapsed
  • collapsingToolbarLayout.setTitleEnabled(true); doesn't seem to have any effect

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

like image 933
VIN Avatar asked Jun 02 '16 19:06

VIN


2 Answers

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.

like image 142
VIN Avatar answered Oct 10 '22 23:10

VIN


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);
like image 25
Apurva Thorat Avatar answered Oct 11 '22 00:10

Apurva Thorat