Listview height not correct - android

I used this method to calculate and set listview height at runtime
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/lv_cart" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"
android:layout_margin="10dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SubTotal:"
android:textSize="18sp"
android:textColor="#000"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rs. 600"
android:layout_marginLeft="5dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/colorPrimary"
android:id="#+id/tv_sub_total"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Order Type:"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:textColor="#4CAF50"
android:textStyle="bold"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Delivery"
android:textAllCaps="false"
android:textSize="18sp"
android:textColor="#ffffff"
android:background="#color/colorPrimary"
android:id="#+id/btn_delivery"/>
<Button
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="PickUp"
android:textAllCaps="false"
android:textSize="18sp"
android:textColor="#ffffff"
android:background="#color/colorPrimary"
android:id="#+id/btn_pickup"/>
</LinearLayout>
</ScrollView>
I debug this method and checked that this method is returning correct height based on children. But when listview is shown then the view in which list is shown is not correct.
I added 8 items in my listview but it only shows 5 when I ran it but height that method returned was correct as it showed 1 view height=51 and 8 view height=415 including divider height.
I also tried listview height in xml with match parent and wrap content but result was same.
What's the problem here?

pleaes try this one :
listView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, the height here));

Related

How to make ExpandableListView scroll inside ScrollView and not on itself?

I can’t get the ExpandableListView to scroll inside the ScrollView and not on itself. The problem is that if I change the ExpandableListView to a TextView with a large text, everything works fine. But the ExpandableListView scroll works without a ScrollView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer"
android:layout_marginBottom="0dp"
android:fillViewport="true"
android:scrollbars="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ImageGoods"
android:layout_width="match_parent"
android:layout_height="250dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/pizza_pepperoni_lovers" />
<TextView
android:id="#+id/ImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="20dp"
android:layout_alignLeft="#id/ImageGoods"
android:layout_alignTop="#id/ImageGoods"
android:text="NEW Product!!!"
android:textStyle="bold"
android:textColor="#color/Black"
android:textSize="30dp"
android:includeFontPadding="false"
android:background="#color/TextGoodsImageColor"
android:gravity="center"/>
<Button
android:id="#+id/SizeButton1"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_marginStart="0dp"
android:layout_marginTop="5dp"
android:background="#drawable/button_border"
android:text="25mm"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="#+id/SizeButton2"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="#+id/textView2"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="#+id/SizeButton1"
android:background="#drawable/button_border"
android:text="30mm"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="#+id/SizeButton3"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_below="#+id/textView2"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="#+id/SizeButton2"
android:background="#drawable/button_border"
android:text="36mm"
android:textAllCaps="false"
android:textSize="12sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ImageGoods"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="size"
android:textColor="#color/Black"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/SizeButton3"
android:layout_marginTop="7dp">
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:transcriptMode="disabled"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/colorFooter"
android:orientation="vertical" >
<Button
android:id="#+id/btn_add_goods_to_basket"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="Add basket"
android:inputType="textFilter"
android:textAlignment="center"
android:layout_alignParentRight="true"
android:textColor="#color/White" />
<Button
android:id="#+id/btn_change_qtty_minus"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_marginLeft="7dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:background="#drawable/plus_minus_button"
android:drawableTop="#drawable/minus"
android:gravity="center_horizontal"
android:padding="5dp"
android:textAlignment="center"
android:textColor="#color/White"
android:textSize="25dp" />
<Button
android:id="#+id/btn_change_qtty_pilus"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_marginStart="7dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
android:layout_toEndOf="#+id/textView5"
android:background="#drawable/plus_minus_button"
android:gravity="center_horizontal"
android:padding="5dp"
android:drawableTop="#drawable/pilus"
android:textAlignment="center"
android:textColor="#color/White"
android:textSize="25dp" />
<TextView
android:id="#+id/TotalSum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btn_change_qtty_minus"
android:layout_alignBottom="#+id/btn_change_qtty_minus"
android:layout_marginStart="7dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="#+id/btn_change_qtty_pilus"
android:textColor="#color/White"
android:gravity="center"
android:text="25$"
android:textStyle="bold" />
<TextView
android:id="#+id/textView5"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btn_change_qtty_minus"
android:layout_alignBottom="#+id/btn_change_qtty_minus"
android:layout_marginStart="7dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_toEndOf="#+id/btn_change_qtty_minus"
android:textStyle="bold"
android:textColor="#color/White"
android:gravity="center"
android:text="x1" />
</RelativeLayout>
</RelativeLayout>
This is my xml code.
How can this problem be solved?
Just found the answer to my question.
enter link description here
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.expandable, container, false);
ExpandableAdapter adapter = new ExpandableAdapter();
expandableListView.setAdapter(adapter);
setExpandableListViewHeight(expandableListView, -1);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
setExpandableListViewHeight(parent, position);
return false;
}
});
return view;
}
private void setExpandableListViewHeight(ExpandableListView listView,
int group) {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
View.MeasureSpec.EXACTLY);
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupItem = listAdapter.getGroupView(i, false, null, listView);
groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += groupItem.getMeasuredHeight();
if (((listView.isGroupExpanded(i)) && (i != group))
|| ((!listView.isGroupExpanded(i)) && (i == group))) {
for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
View listItem = listAdapter.getChildView(i, j, false, null,
listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
int height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
if (height < 10)
height = 200;
params.height = height;
listView.setLayoutParams(params);
listView.requestLayout();
}

Change ListView Height at Runtime

I am trying to implement a nested ListView at the bottom of my UI but only one item of my array is populating the screen. I've figured out that I can display all of my items if I set my Height attribute (i.e. "175dp" in lieu of "Wrap_Content"). The problem is the list size is not static, so I need to adjust the height of my ListView at runtime.
I've dug around the internets and here on StackOverflow but I can't seem to find a solution.
Thanks!
Code from my fragment:
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(rootView.getContext(),
android.R.layout.simple_list_item_1, list);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rootView.findViewById(R.id.list_linear).getLayoutParams();
LinearLayout manager = (LinearLayout) rootView.findViewById(R.id.list_linear);
lp.height = 45 * values.length;
manager.setLayoutParams(lp);
listView.setAdapter(adapter);
My XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BBDEFB"
android:padding="3dp"
android:id="#+id/movie_description_container">
<ImageView
android:layout_width="match_parent"
android:layout_height="225dp"
android:id="#+id/movie_detail_hero_image"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:gravity="top"
android:src="#drawable/sample_1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="movie title"
android:id="#+id/movie_detail_title"
android:layout_alignBottom="#+id/movie_detail_hero_image"
android:backgroundTint="#color/background_floating_material_dark"
android:textSize="36dp"
android:textColor="#BBDEFB"
android:background="#a3000000"
android:layout_above="#id/movie_detail_hero_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:paddingLeft="18dp"/>
<ImageView
android:layout_width="175dp"
android:layout_height="215dp"
android:id="#+id/movie_detail_poster"
android:src="#drawable/sample_0"
android:layout_below="#+id/movie_detail_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="12dp"
android:gravity="left"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="date"
android:textSize="22dp"
android:id="#+id/movie_detail_date"
android:layout_centerVertical="true"
android:layout_below="#id/movie_detail_hero_image"
android:layout_alignRight="#+id/movie_detail_hero_image"
android:layout_marginRight="12dp"
android:layout_marginTop="32dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Rating:"
android:textSize="22dp"
android:id="#+id/movie_detail_rating"
android:layout_below="#+id/movie_detail_date"
android:layout_alignRight="#+id/movie_detail_date"
android:layout_alignEnd="#+id/movie_detail_date"
android:layout_marginTop="6dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:numStars="4"
android:nestedScrollingEnabled="true"
android:maxWidth="77dp"
android:layout_below="#+id/movie_detail_rating"
android:layout_alignRight="#+id/movie_detail_rating"
android:layout_alignEnd="#+id/movie_detail_rating"
android:layout_marginTop="3dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Synopsis"
android:id="#+id/movie_detail_synopsis_header"
android:layout_below="#+id/movie_detail_poster"
android:layout_alignLeft="#+id/movie_detail_poster"
android:layout_alignStart="#+id/movie_detail_poster" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="description"
android:id="#+id/movie_detail_synopsis"
android:layout_below="#+id/movie_detail_synopsis_header"
android:layout_alignLeft="#+id/movie_detail_synopsis_header"
android:layout_alignStart="#+id/movie_detail_synopsis_header"
android:layout_margin="6dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton_Favorite"
android:layout_below="#+id/ratingBar"
android:layout_alignRight="#+id/ratingBar"
android:src="#drawable/ic_favorite_white_24dp"
android:maxWidth="45dp"
android:maxHeight="45dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_linear"
android:layout_below="#id/movie_detail_synopsis">
<ListView
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="#+id/list_view2"/>
</LinearLayout>
Actually you can set the height of listview to the size of childers with this method
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
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,
LayoutParams.WRAP_CONTENT));
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}

List view displaying only one record when Android scroll view is used

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);
}

Extra space While adding Grid View to ScrollView

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();
}

Two listview inside scrollView

I have two listview. i need to scroll one listview as the continuation of another list view. In between the list there should be a text view. if i use the two listview and textview inside a scrollview icould not able to scroll both the list. if i use the listview without scrollview i can see the two listview but scrolling is not continues.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FAF0BE" >
<Button
android:id="#+id/btnheader"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#A1CAF1"
android:text="DisoderDetail" />
<TextView
android:id="#+id/txtTmtDnam"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/btnheader"
android:layout_marginLeft="#dimen/marginLLeft"
android:layout_marginTop="#dimen/marginTopp"
android:text="jgjggjh"
android:textColor="#000000" />
<TextView
android:id="#+id/txtdisDdtail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtTmtDnam"
android:layout_marginLeft="#dimen/marginLLeft"
android:layout_marginTop="#dimen/marginTopp"
android:text="jgjggjh hjhhhjh jhjhh"
android:textColor="#000000" />
<RelativeLayout
android:id="#+id/Rvlytt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/lyt2"
android:layout_below="#id/txtdisDdtail"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/marginBottomm"
android:layout_marginTop="#dimen/marginTop"
android:orientation="horizontal" >
<ImageView
android:id="#+id/starimg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:onClick="star1Clicked"
android:src="#drawable/starunselected"
android:tag="1" />
<ImageView
android:id="#+id/starimg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg1"
android:onClick="star2Clicked"
android:src="#drawable/starunselected"
android:tag="2" />
<ImageView
android:id="#+id/starimg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg2"
android:onClick="star3Clicked"
android:src="#drawable/starunselected"
android:tag="3" />
<ImageView
android:id="#+id/starimg4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/starimg3"
android:onClick="star4Clicked"
android:src="#drawable/starunselected"
android:tag="4" />
</RelativeLayout>
<ScrollView
android:id="#+id/Scrlvw"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#id/Rvlytt2" >
<RelativeLayout
android:id="#+id/rvl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtDcmt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="#dimen/marginTop"
android:text="comment"
android:textColor="#000000" />
<ListView
android:id="#+id/lstvwDis11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtDcmt" >
</ListView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rvl2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/rvl1"
>
<TextView
android:id="#+id/txtDrate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/lstvwDis11"
android:layout_marginTop="#dimen/marginTop"
android:text="Rate"
android:textColor="#000000" />
<ListView
android:id="#+id/lstvwDis12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/txtDrate" >
</ListView>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/Rvlt22"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/Edtcmmt"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:background="#ffffff"
android:hint="Add a comment"
android:textColor="#000000" />
<Button
android:id="#+id/btnpost"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/Edtcmmt"
android:onClick="OnPostbtnclick"
android:text="Post" />
</RelativeLayout>
</RelativeLayout>
This is not correct design for starters but if you want to stick with the design one quick fix that will make your code work is to put both of your listViews inside one more list view and put that under scroll view. that should solve your problem of scrolling.
When placing ListView inside ScrollView I use the following method to set listView height and the scrolling works properly. Use this after you've added all views to your listview.
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);
}
}

Categories

Resources