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.setTitleEnabled(true);
doesn't seem to have any effectMainActivity.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
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With