I have a layout where I have to add something on top and then at bottom on list view so I don't want to scroll only I want to scroll only list view I want to scroll whole screen so I added this code.
//Remove scrolling from list view so that It scroll whole screen
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ActionBar.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
here is my xml
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/header"
android:orientation="horizontal"
android:layout_alignLeft="#+id/body"
android:layout_marginBottom="80dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<LinearLayout
android:id="#+id/body"
android:orientation="vertical"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/header"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingTop="10dp"
>
<ScrollView
android:layout_width="match_parent"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:weightSum="2"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:text="RSHO Doses"
android:layout_weight="1"
android:textColor="#636363"
android:textSize="#dimen/sub_Heading_Size"
android:drawablePadding="10dp"
android:drawableLeft="#drawable/rsho_drawable"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="#dimen/sub_Heading_Size"
android:text="Seizures"
android:drawablePadding="10dp"
android:drawableLeft="#drawable/seizures_drawable"
android:textColor="#636363"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:text="Events"
android:padding="#dimen/activity_horizontal_margin"
android:background="#color/colorHeadingBG"
android:textSize="#dimen/sub_Heading_Size"
android:textColor="#color/colorHeading"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:dividerHeight="2dp"
android:divider="#e5e5e5"
android:layout_alignParentTop="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_horizontal_margin"
android:id="#+id/listView"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
so when I open this application it remove scrolling from list view and add whole list view height in page but it always open from bottom. any solution for this
Try using this setting.
listView.setNestedScrollingEnabled(false);
listView.setHasFixedSize(false);
This way the scroll of ListView will be disabled.
Related
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="100"
android:background="#drawable/round_shape"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="25">
<TextView
android:text="Title"
android:paddingStart="20dp"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/borrowBookTitle"
android:maxLines="1"
android:textSize="25sp"
android:textColor="#000000"
/>
<TextView
android:text="Locate"
android:paddingStart="20dp"
android:ellipsize="end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/locate"
android:maxLines="2"
android:textSize="15sp"
android:textColor="#838383"
/>
<TextView
android:text="borrowStartDate"
android:layout_marginStart="10dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="8dp"
android:paddingStart="10dp"
android:paddingEnd="20dp"
android:paddingBottom="5dp"
android:layout_width="wrap_content"`enter code here`
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/borrowStartDate"
android:maxLines="1"
android:textSize="17sp"
android:textColor="#000000"
android:background="#color/childview_background"
/>
<TextView
android:text="borrowEndDate"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:paddingStart="10dp"
android:paddingEnd="20dp"
android:paddingBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/borrowEndDate"
android:visibility="gone"
android:maxLines="1"
android:textSize="17sp"
android:textColor="#000000"
android:background="#color/childview_background"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="75">
<TextView
android:text="D-11"
android:paddingStart="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/borrowDday"
android:textSize="35sp"
android:textColor="#000000"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/button1"
android:id="#+id/renewBtn"
android:textSize="20sp"
android:textColor="#000000"
android:visibility="gone"
android:text="RENEW"
/>
</LinearLayout>
Above xml files is Listview's item xml file.
borrowStartDate, borrowEndDate and renewBtn visiblility is gone.
But When click on item, change gone to visible
borrowListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
TextView startDate = (TextView)view.findViewById(R.id.borrowStartDate);
TextView endDate = (TextView)view.findViewById(R.id.borrowEndDate);
Button renewBtn = (Button)view.findViewById(R.id.renewBtn);
if ( startDate.getVisibility() == View.GONE) {
startDate.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_down_and_fade_in));
endDate.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_down_and_fade_in));
renewBtn.startAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_in));
startDate.setVisibility(View.VISIBLE);
endDate.setVisibility(View.VISIBLE);
renewBtn.setVisibility(View.VISIBLE);
}
else {
startDate.setVisibility(View.GONE);
endDate.setVisibility(View.GONE);
renewBtn.setVisibility(View.GONE);
}
}
});
It's change visibility code
Problem is Listview height matched When Items Gone's height.
So When I clicked each item not directly showing under parts.
I want to When click on items, Listview height automatically expand
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<WebView
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
android:id="#+id/rightWebView"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh"
android:backgroundTint="#color/button1"
android:drawableEnd="#drawable/refresh_picto"
android:id="#+id/refreshBorrowBook"
android:onClick="refreshBorrow"
android:layout_marginBottom="10dp"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/borrowBookList"
android:dividerHeight="10dp"
android:divider="#color/main_background"
/>
<TextView
android:text="예약 목록"
android:paddingStart="50dp"
android:visibility="gone"
android:layout_marginTop="15dp"
android:layout_marginBottom="20dp"
android:textColor="#000000"
android:textSize="25sp"
android:background="#drawable/round_shape"
android:backgroundTint="#color/childview_background"
android:id="#+id/reservation_list_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reservationBookList"
android:dividerHeight="10dp"
android:divider="#color/main_background"
/>
</LinearLayout>
</ScrollView>
This is fragments code
Add snapshots
after refresh button after click refresh button
when click item when click item
If scrolling down, then showing listview's other parts
I don't want to scrolling.
Just When click items then listview get heights automaticially.
I think this problem is not relative with notifyDataSetChanged();
Use
view.getLayoutParams().Height=value
You don't have to toggle visibility by the way
Hope that helps
Below method calculate each Child View height , You must call below method after pressing button of any child view in Listview to visible and invisible.
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
Call this function right after you change ListView items, like that:
Utility.setListViewHeightBasedOnChildren(myListView);
In android, I would like to show a UI with fixed text and dynamic list. I added a scroll view, but it only scroll the listview(id is listview_name), not the whole view. How could I fix this issue?
Here is my code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="colorBlack"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listview_name"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="colorBlack"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
You can use ListView inside Scrollview, I have tried following method to achieve this.
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0)
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, RelativeLayout.LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
Usage in activity : setListViewHeightBasedOnChildren(yourlistview);
I am a windows developer. I don't have much knowledge in android. So my scenario is like this: I have a scrollview which has its child as a linearlayout. The Linear layout has 3 listviews. I want to show all the items in all of the 3 listviews. The scrolling will be handled by the top level scrollview.
Will this work ?
<ScrollView
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView1"
android:fadeScrollbars="true">
<LinearLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout2">
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/linear_currentSubEmpty"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/currentListViewLayout"
android:layout_below="#+id/currentSubsView">
<com.fortysevendeg.swipelistview.SwipeListView xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="#+id/currentSubList"
android:listSelector="#00000000"
android:layout_width="match_parent"
android:layout_height="match_parent"
swipe:swipeFrontView="#+id/front"
android:background="#android:color/transparent"
swipe:swipeBackView="#+id/back"
swipe:swipeDrawableChecked="#drawable/choice_selected"
swipe:swipeDrawableUnchecked="#drawable/choice_unselected"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeMode="left"
swipe:swipeActionLeft="reveal"
swipe:swipeOpenOnLongPress="false"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/pastViewLayout"
android:layout_height="match_parent">
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/linear_pastSubEmpty"
android:layout_height="wrap_content"
android:layout_below="#+id/pastBottomView">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pastSubList"
android:divider="#ffffffff"
android:dividerHeight="1dp"
android:layout_weight="1" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/suspViewLayout"
android:layout_height="match_parent">
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/linear_suspendedSubEmpty"
android:layout_height="wrap_content"
android:layout_below="#+id/suspBottomView">
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:id="#+id/suspSubList"
android:divider="#ffffffff"
android:dividerHeight="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
I got to know that there is no direct way to achieve this, I followed the following method.
public static void SetListViewHeightBasedOnChildren(ListView listView) {
IListAdapter listAdapter = listView.Adapter;
if (listAdapter == null) {
return;
}
int totalHeight = listView.PaddingTop + listView.PaddingBottom;
for (int i = 0; i < listAdapter.Count; i++) {
View listItem = listAdapter.GetView(i, null, listView);
if (listItem is ViewGroup) {
listItem.LayoutParameters = new global::Android.Views.ViewGroup.LayoutParams (global::Android.Views.ViewGroup.LayoutParams.WrapContent, global::Android.Views.ViewGroup.LayoutParams.WrapContent);
}
listItem.Measure(0, 0);
totalHeight += listItem.MeasuredHeight;
}
global::Android.Views.ViewGroup.LayoutParams parameters = listView.LayoutParameters;
parameters.Height = totalHeight + (listView.DividerHeight * (listAdapter.Count - 1));
listView.LayoutParameters = parameters;
}
I am new to android and I am displaying a ListView getting data from a webservice. Below listview there are some other views/controls on the activity.Now the issue I am getting is that when the webservice returns more than one record controls below the listview are getting hidden at the bottom of window for that I tried to use scrollView in my android activity but the issue I am getting after using scroll view is that only one record is getting displayed in the listview. So the problem is 2 way :
when I implement listview with other controls at the bottom of it I am not able to scroll when listview displays multiple records.
when I use scrollView with and put the listview and other controls inside scrollview , listview displays only one record.
My xml for this is like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kanix.highrise.com.highrise.generate_material_requisition">
<!-- TODO: Update blank fragment layout -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lstTaskQuantity"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Req. From :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:id="#+id/etDate"
android:layout_weight=".5"
android:hint="DD/MM/YYYY"/>
<ImageButton
android:layout_width="35dp"
android:layout_height="35dp"
android:id="#+id/btnimgcal"
android:src="#drawable/calender"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsdfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="For Days :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="#+id/editText"
android:layout_weight="1.69" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="#+id/txtLddsddfgfadsbel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:text="Quantity :"
android:layout_weight="1"
android:textColor="#fff" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:singleLine="true"
android:width="70dp"
android:id="#+id/etQuantity"
android:layout_weight="1.60" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0080FF"
android:background="#fff"
android:text="Generate Requisition"
android:id="#+id/btnSave" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
You shouldn't put a listview in scrollview if still you want to place then set the height of listview programatically with this method.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = listView.getPaddingTop()
+ listView.getPaddingBottom();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem instanceof ViewGroup) {
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
I am using GridView which is inside scrollview(i know bad practice but requirements are like this only). The problem is their is unwanted extra vertical space coming at the end of GridView. I am also setting the height of GridView dynamically by this code.
public static void setHeightDynamically(GridView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
listView.requestLayout();
}
My xml is like this
<RelativeLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/header_section"
layout="#layout/header_screen" />
<ScrollView
android:id="#+id/parent_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:layout_alignWithParentIfMissing="false"
android:layout_below="#+id/header_section">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="200dp" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
></ListView>
<TextView
android:id="#+id/more"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#android:color/darker_gray"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="14sp"
android:visibility="gone" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textColor="#android:color/black"
android:textSize="14sp" />
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/txt"
android:scaleType="fitXY"
android:layout_margin="5dp"
/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/txt"
android:layout_margin="5dp"
android:layout_marginLeft="3dp"
android:layout_toRightOf="#+id/icon"
android:textColor="#android:color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/demo_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/icon"
android:textColor="#android:color/black"
android:textSize="12sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
<GridView
android:id="#+id/items_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
<include layout="#layout/nav_drawer" />
Make increment according to the number of columns in your grid view , here i am assuming number of coulmns are 2 so just Make an increment of +2 in your loop
public static void setHeightDynamically(GridView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i = i+2) {
view = listAdapter.getView(i, view, listView);
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
listView.requestLayout();
}