24

I want to hide my AppBarLayout for adding a view dynamically which will take the height of the screen. For this, i want to remove a view temporaly by setting the visibility of my AppBarLayout to GONE. The view is not visible, but take space in the screen(half of the height of screen).

My XML code :

<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/coordinatorFriendProfil"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#81D4FA"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/fProfilAppBar"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/fProfilCollapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#B3E5FC"
        android:gravity="center"
        android:orientation="vertical"
        app:contentScrim="#03A9F4"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <LinearLayout
            android:id="@+id/fProfilUserInfo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <com.neden.neden.RoundedImageView
                android:id="@+id/ivFriendPicture"
                android:layout_width="150sp"
                android:layout_height="150sp"
                android:layout_gravity="center"
                android:fitsSystemWindows="true"
                android:src="@drawable/photo"
                app:border_color="#64B5F6"
                app:border_width="4dp"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7" />

            <TextView
                android:id="@+id/fProfilName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="15pt" />

        </LinearLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/fProfilToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways" />

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

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

<FrameLayout
    android:id="@+id/fProfilContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Okn
  • 698
  • 10
  • 21

4 Answers4

32

Your problem is caused by this line

    app:layout_behavior="@string/appbar_scrolling_view_behavior"

You will have to set it programatically on CoordinatorLayout not the actual container fragment that has that field.

Here is how

    mContainer = (FrameLayout) findViewById(R.id.main_content_container);

    CoordinatorLayout.LayoutParams params =
            (CoordinatorLayout.LayoutParams) mContainer.getLayoutParams();
    params.setBehavior(null);

    mContainer.requestLayout();

If you want to make it scrolling again

   params.setBehavior(new AppBarLayout.ScrollingViewBehavior());
Adam Fręśko
  • 1,064
  • 9
  • 14
7

Try to set AppBarLayout's height to zero

AppBarLayout appBar=(AppBarLayout)findViewById(R.id.fProfilAppBar);
LinearLayout.LayoutParams params=appBar.getLayoutParams();
params.height=0;
appBar.setLayoutParams(params);
mohax
  • 4,435
  • 2
  • 38
  • 85
  • It work but when i do this, my FrameLayout doesn't take match_parent for the layout_height – Okn Sep 21 '15 at 22:18
  • What do you mean saying "doesn't take match_parent for the layout_height"? It doesnt fit screen? – mohax Sep 21 '15 at 22:21
  • I'm sorry for the English. I want FrameLayout take all height of the screen, but when i set the appBarLayout, the FrameLayout is at the top of the screen but not at bottom (like a margin_bottom but i can't see any margin in my code). For simplify, i want android:layout_height:"match_parent" – Okn Sep 21 '15 at 22:25
  • This maybe caused by `android:fitsSystemWindows="true"`. Try to add/remove it from `CoordinatorLayout`/`FrameLayout`. Or just add `layout_marginTop` to `FrameLayout` – mohax Sep 21 '15 at 22:32
  • I tried with all combinations but its not working with android:fitsSystemWindows="true" – Okn Sep 21 '15 at 22:46
  • Try to set height of `FrameLayout` to `LayoutParams.MATCH_PARENT` after setting `AppBarLayout` height to zero. For `FrameLayout` use `FrameLayout.LayoutParams` – mohax Sep 21 '15 at 22:49
0

I was able to solve this by moving everything from CollapsingToolbarLayout (CTL) into Toolbar, making Toolbar the only desdendant of CTL. Then I set the CTL's height to wrap_content and in the code I used toolbar.setVisibility(View.GONE). Note that you might want to change the layout inside Toolbar, add some wrapper around its childs, like RelativeLayout or something else.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/activity_main_root_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/default_page_background_color"
        android:orientation="vertical"
        tools:context=".home.ui.MainActivity">

        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/page_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/default_page_background_color"
            android:fitsSystemWindows="true"
            app:layout_constraintBottom_toTopOf="@id/home_nav_tabs"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="false"
                android:orientation="vertical"
                android:theme="@style/AppTheme.AppBarOverlay"
                app:elevation="0dp">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:contentScrim="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll|enterAlways|snap"
                    app:scrimAnimationDuration="300">

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:buttonGravity="top"
                        app:layout_collapseMode="none"
                        app:popupTheme="@style/AppTheme.PopupOverlay">

                        ...
arenaq
  • 2,304
  • 2
  • 24
  • 31
0

I have tried these, still not working. better solution is show and hide the CollapsingToolbar and set the AppBarLayout params dynamically.

Do this when ever you show/hide the CollapsingToolbarLayout

mAppBar.setLayoutParams(**new CoordinatorLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)**);