Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapsing toolbar is collapsed by default

I have a problem with CollapsingToolbar. The problem in that by default I need to have collapsed toolbar when Activity just has been created. If I do that through the code with appBarLayout.setExpanded(false, false) in onCreate() method, I see expanded toolbar for the first time and then it collapsed in a second. I need to remove that and display collapsed toolbar immediately.

UPD:

<android.support.design.widget.AppBarLayout
    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/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:fitsSystemWindows="true">

  <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="@color/colorPrimary"
      app:expandedTitleMarginEnd="64dp"
      app:expandedTitleMarginStart="16dp"
      app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <ImageView
        android:id="@+id/toolbar_image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:fitsSystemWindows="true"
        tools:ignore="ContentDescription"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@android:color/transparent"
        app:layout_collapseMode="pin">
    </android.support.v7.widget.Toolbar>
  </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
like image 814
Near1999 Avatar asked Aug 15 '16 15:08

Near1999


People also ask

How do I stop my Toolbar from collapsing?

Solution: The solution is simple, we just need to set the app:scrimAnimationDuration=”0" in our collapsing toolbar layout like the below code snippet. Now just run the code and see the results, you will see then there will be no fading animation anymore.

How do I know if my collapsing Toolbar is collapsed?

To detect if fully collapsed check if Math. abs(offset) == appBarLayout.

What is collapsing Toolbar?

Android CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout. This type of layout is commonly seen in the Profile Screen of the Whatsapp Application.


1 Answers

AppBarLayout has an expanded attribute. Try setting that to false.

<android.support.design.widget.AppBarLayout
    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/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:fitsSystemWindows="true"
    app:expanded="false">

This worked for me in my test project when I plugged in your layout and added that one line.

I was able to do this with versions 23.0.1 and 23.4.0 of the design support library.

like image 66
kris larson Avatar answered Sep 21 '22 19:09

kris larson