mrid
mrid

Reputation: 5796

How to adjust recyclerview height?

I want the recyclerview to adjust it's height so that the items don't get hidden behind the pricebar ( see image )

This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutMainContainer"
    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"
    tools:context="com.mridulahuja.groceryapp.activities.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@null" />

    <RelativeLayout
        android:id="@+id/layoutPricebar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#454560"
        android:layout_alignParentBottom="true"
        android:visibility="gone">

        ...
        ...

    </RelativeLayout>

</RelativeLayout>

Code I'm using to resize recyclerview:

ViewTreeObserver pricebarvto = layoutPricebar.getViewTreeObserver();
pricebarvto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            layoutPricebar.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            layoutPricebar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        pricebarHeight = layoutPricebar.getMeasuredHeight();
    }
});


RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) recyclerView.getLayoutParams();
params.height = relativeLayoutContainerHeight - pricebarHeight;
recyclerView.setLayoutParams(params);

where relativeLayoutContainerHeight is the height of the root RelativeLayout

But the height isn't changing. It's changing when I hard-code a big value to pricebarHeight such as 160. And I've given pricebar the height of just 50dp.

Also I'm using this animation to display the pricebar:

<translate
xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromYDelta="180%"
    android:toYDelta="0%">
</translate>

enter image description here

Upvotes: 0

Views: 1313

Answers (5)

Jyoti  Sharma
Jyoti Sharma

Reputation: 233

Use linear layout with vertical orientation instead of relative layout and give recycler view

android:layout_weight="1"

Upvotes: 2

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

If you use RelativeLayout then you should use android:layout_marginBottom

Specifies extra space on the bottom side of this view. This space is outside this view's bounds. Margin values should be positive.

XML

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        ................
        android:layout_marginBottom="50dp" />

If you want LinearLayout Then follow android:weightSum Logic .

Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

Upvotes: 1

Pranav Ashok
Pranav Ashok

Reputation: 591

if your pricebar height is 50dp make RecyclerView android:layout_marginBottom="50dp" this will solve your issue no need of java code for resizing the Recyclerview

Upvotes: 0

Jason
Jason

Reputation: 132

Try to put the layout pricebar with id : layoutPricebar below the recycler_view by using android:layout_below: "@+id/recycler_view"

 <RelativeLayout
    android:id="@+id/layoutPricebar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#454560"
    android:layout_below="@+id/recycler_view"
    android:layout_alignParentBottom="true"
    android:visibility="gone">

Upvotes: 0

Sahil Manchanda
Sahil Manchanda

Reputation: 10567

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutMainContainer"
    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"
    tools:context="com.mridulahuja.groceryapp.activities.MainActivity">

    <RelativeLayout
        android:id="@+id/layoutPricebar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#454560"
        android:layout_alignParentBottom="true"
        android:visibility="gone">

        ...
        ...

    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_above = "@id/layoutPricebar"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:background="@null" />



</RelativeLayout>

Upvotes: 1

Related Questions