Reputation: 397
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>
Upvotes: 5
Views: 14214
Reputation: 485
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
Upvotes: 1
Reputation: 624
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.
Upvotes: 14
Reputation: 3310
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);
}
Upvotes: 1
Reputation: 105
WRAP_CONTENT will not work if you want recyclerView to take part of screen. If you want other content to be there below recyclerView, you can put recyclerView inside LinearLayout and use the following code to change the height dynamically.
I assume you are using a list(complaint_list) for storing complaints.Adjust the complaint_height accordingly
ViewGroup.LayoutParams params = mRecyclerView.getLayoutParams();
int complaint_height=400;
if (complaints_list.size() > 2) {
params.height = ((complaint_list.size() - 1) * 100) + complaint_height;
} else {
params.height = complaint_height+100;
}
mRecyclerView.setLayoutParams(params);
Upvotes: -1