Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase the height of recyclerview dynamically on item add

Display pic

In my app, i want to increase the height of recycler view dynamically. As i add new item under complaints, the height of recycler view should increase. Is there a way to do it? Please help me with this. Thanks in advance.

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/complaint_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_line"
            android:padding="@dimen/layout_margin"
            android:text="Complaints"
            android:textColor="@color/textColorFullBlack" />

        <LinearLayout
            android:id="@+id/complaints_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="@dimen/layout_margin"
            android:layout_weight="1">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/complaint_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/layout_margin"
                android:divider="@color/toolBarBackground"
                android:groupIndicator="@android:color/transparent" />

        </LinearLayout>

        <TextView
            android:id="@+id/instructions_text_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/background_line"
            android:padding="@dimen/layout_margin"
            android:text="Instructions"
            android:textColor="@color/textColorFullBlack" />

        <FrameLayout
            android:id="@+id/instructions_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="@dimen/layout_margin"
            android:layout_weight="1">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/instructions_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/layout_margin"
                android:divider="@color/toolBarBackground"
                android:groupIndicator="@android:color/transparent" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@color/colorPrimary"
                android:orientation="horizontal">

                <in.palmpower.videocon.uicommon.widget.EditText
                    android:id="@+id/search_text_field"
                    style="@style/FormEditText"
                    android:layout_width="0dp"
                    android:layout_gravity="center_vertical"
                    android:layout_margin="@dimen/one_dp"
                    android:layout_weight="1"
                    android:background="@android:color/white"
                    android:inputType="textFilter"
                    android:padding="@dimen/search_edit_text_padding"
                    android:privateImeOptions="nm" />

                <Button
                    android:id="@+id/add_button"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_vertical"
                    android:background="?attr/selectableItemBackground"
                    android:contentDescription="@string/edittext_hint_search"
                    android:padding="@dimen/button_padding"
                    android:text="@string/add_button_string"
                    android:textColor="@color/textColorWhite" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>
like image 964
Harshith Avatar asked Oct 04 '16 09:10

Harshith


3 Answers

Got stuck on same issue, i was using RecyclerView with wrap_content inside LinearLayout, i change LinearLayout to RelativeLayout and it set RecyclerView Height Based On Children.

like image 78
Syed Arsalan Shah Avatar answered Oct 12 '22 18:10

Syed Arsalan Shah


This will work for you

RecyclerView mRecyclerView 
             = (RecyclerView) findViewById(R.id.complaint_recycler_view);
int originalItemSize = iteList.size();

ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams();

if (itemList.size() > originalItemSize ){
    params.height = params.height + 100;
    originalItemSize  = itemList.size();
    mRecyclerView.setLayoutParams(params); 
}
like image 1
Jai Avatar answered Oct 12 '22 18:10

Jai


i suggest you to put teh recyclerview in any other layout (Relative layout is preferable). Then change recyclerview's height/width as match parent to that layout and set teh parent layout's height/width as wrap content. it works for me

like image 1
murugan mani Avatar answered Oct 12 '22 19:10

murugan mani