Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - RelativeLayout height to fill remaining space of CollapsingToolBarLayout

I have the following design where there is a CollapsingToolbarLayout on top and below there is a RelativeLayout:

enter image description here

I want the RelativeLayout to fill the remaining space until bottom so I can center the content like this:

enter image description here

I tried using match_parent on the RelativeLayout but it takes the size of the whole root layout, including the CollapsingToolbarLayout:

enter image description here

What can I do? This is the complete layout's code:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".UserActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">

    <android.support.design.widget.AppBarLayout android:id="@+id/activity_user_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/activity_user_collapsingtoolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar android:id="@+id/activity_user_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_collapseMode="pin">

                <!-- toolbar content -->

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

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

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout android:id="@+id/activity_user_eventsalert"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/calendar_remove_titlegray_48px"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="No events yet"
                android:textSize="20sp"
                android:textColor="@color/colorTitleGray"/>
        </LinearLayout>

    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_gravity="bottom"
        android:background="@drawable/shadow_bottom_white_to_transparent"/>

</android.support.design.widget.CoordinatorLayout>
like image 437
Alex Ardavin Avatar asked May 29 '17 23:05

Alex Ardavin


1 Answers

Try Appbar Scroll listener

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

    mAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                  int appbarHeight=appBarLayout.getHeight(); // or measured height
                  int totalHeightOfDevice=height;
                  int absoluteHeight=height-appbarHeight;

                  // position your view to absolute height..
            }
        });
like image 60
Nirav Shah Avatar answered Oct 29 '22 23:10

Nirav Shah