Set Starting Height of Collapsingtoolbarlayout

Set starting height of CollapsingToolbarLayout

cheesesquare implement your Request

this is a layout in cheesesquare

<?xml version="1.0" encoding="utf-8"?>
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">

<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />

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

</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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/card_margin">

<LinearLayout
style="@style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cheese_ipsum" />

</LinearLayout>

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/card_margin"
android:layout_marginLeft="@dimen/card_margin"
android:layout_marginRight="@dimen/card_margin">

<LinearLayout
style="@style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Friends"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cheese_ipsum" />

</LinearLayout>

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/card_margin"
android:layout_marginLeft="@dimen/card_margin"
android:layout_marginRight="@dimen/card_margin">

<LinearLayout
style="@style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Related"
android:textAppearance="@style/TextAppearance.AppCompat.Title" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cheese_ipsum" />

</LinearLayout>

</android.support.v7.widget.CardView>

</LinearLayout>

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

<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end"
android:src="@drawable/ic_discuss"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"/>

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

Set initial height of parallax image in CollapsingToolbarLayout

Here is one hacky way to accomplish this, I am not sure of a better way but would love to hear it. Check out ControllableAppBarLayout

Place that code in your project and replace:

    android.support.design.widget.AppBarLayout

With:

   ControllableAppBarLayout

Set the android:layout_height of ControllableAppBarLayout to be the maximum size of your toolbar, i.e. the size you want in your "scrolling further down" picture.

Then, in your code in the onCreate() method, you can get access to the ControllableAppBarLayout:

appBarLayout = (ControllableAppBarLayout) findViewById(R.id.appBarLayout);
appBarLayout.collapseToolbar();

appBarLayout.collapseToolbar() will completely close the toolbar. However, you can modify this method in ControllableAppBarLayout:

private void performCollapsingWithoutAnimation() {
if (mParent.get() != null) {
mBehavior.onNestedPreScroll(mParent.get(), this, null, 0, getHeight(), new int[]{0, 0});
}
}

specifically, change getHeight() to the number in dp representing the difference between the max height of the toolbar image and the height you want to initially show. That is, the difference between "initial state" and "scrolling further down" as you showed above.

This hack should accomplish what you want.

CollapsingToolbarLayout open with min height when image header contains large image

I got the answer with trial and error with above code of the setAppBarOffset method.
In this method, I was setting offset which I want to keep visible on page load. Instead, we need to pass the offset which we need to hide. So I have added below code to keep 256 px of the image visible on creating.

public static int getDeviceHeight(Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int height = metrics.heightPixels;

Rect rectgle = new Rect();
Window window = ((Activity) context).getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight = rectgle.top;
height = height - StatusBarHeight;
return height;
}

public static int getMinHeightOfAppbar(int device_height,int view_height){
int offsetPx = (int) (device_height * 0.45);
offsetPx = view_height - offsetPx;
return offsetPx;
}

int device_height = Utility.getDeviceHeight(ProfileSettingsNew.this);
offsetPx= Utility.getMinHeightOfAppbar( device_height,imgv_user_photo.getHeight());

setAppBarOffset(offsetPx);

How can i set the minimum height of a collapsingToolbar in android?

Set bottom margin of your Toolbar to whatever space you want to be reserved in the collapsed version.

Source - setting minHeight in CollapsingToolbarLayout not having any effect

Programmatically set height of collapsingToolbarLayout

Actually, you probably just want to add android:adjustViewBounds="true" to your ImageView. As per the documentation of adjustViewBounds

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

If you leave it false (the default), then the ImageView will not resize when Glide later loads the image.

CollapsingToolbarLayout with wrap_content height overlaps views

Try this way:-

   <android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false"
app:toolbarId="@+id/toolbar">

<RelativeLayout
android:id="@+id/profile_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:layout_collapseMode="none">

<ImageView
android:id="@+id/profile_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="10dp"
android:src="@mipmap/ic_launcher"
tools:ignore="ContentDescription" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:layout_toEndOf="@id/profile_img"
android:text="@string/laurent_koscielny"
android:textColor="@android:color/black"
android:textSize="20sp"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/profile_img" />

</RelativeLayout>

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

<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_gravity="bottom"
android:background="@android:color/transparent"
app:tabGravity="fill"
app:tabIndicatorColor="@android:color/holo_orange_dark"
app:tabMaxWidth="0dp"
app:tabMode="fixed"
app:tabSelectedTextColor="@android:color/black"
app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
app:tabTextColor="@android:color/black" />

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

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

</android.support.v4.view.ViewPager>

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


Related Topics



Leave a reply



Submit