Change ListView Height at Runtime - android

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

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

Set ListView Height programmatically in Android

I have a ListView in my activity's Layout that exists in a ScrollView like that:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:orientation="vertical"
android:padding="4dp">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:visibility="gone" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/article_title"
android:gravity="center_vertical|center_horizontal"
android:padding="5dp"
android:text="#string/add_to_my_resources"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
</LinearLayout>
<LinearLayout
android:id="#+id/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:id="#+id/article_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/article_title"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
<Button
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/white" />
</LinearLayout>
<ListView
android:id="#+id/authors_listView"
android:layout_width="wrap_content"
android:layout_height="match_parent">
</ListView>
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:divider="#color/light_gray"
android:dividerHeight="0.5dp"
android:groupIndicator="#null"
android:scrollbars="none"
android:textAlignment="center">
</ExpandableListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
And I have a List Adapter names MyListAdapter that populate my listview:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = mcontex.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.my_list, null);
holder = new ViewHolder();
holder.txtViewName = (TextView) convertView.findViewById(R.id.textView1);
holder.txtViewAff = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewName.setGravity(Gravity.LEFT);
holder.txtViewName.setText(name.get(position));
if (aff != null) {
holder.txtViewAff.setGravity(Gravity.LEFT);
holder.txtViewAff.setText(aff.get(position));
}
return convertView;
}
And my_list.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="#+id/textView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
app:srcCompat="#drawable/ic_draw"/>
</RelativeLayout>
I want to set listview height dynamically to avoid scrolling list view inside the parent layout. So I use this method:
public static void setListViewHeightBasedOnChildren(ListView listView) {
AuthorsListAdapter listAdapter = (AuthorsListAdapter) 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,
LinearLayoutCompat.LayoutParams.MATCH_PARENT));
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + ((listView.getDividerHeight()) * (listAdapter.getCount()));
listView.setLayoutParams(params);
listView.requestLayout();
}
This method works correctly in normal but when txtViewAff value is more than 2 or 3 lines, the last item of listview goes behind of the ExpandableListView. I add constant value like 40 to my totalHeight in method setListViewHeightBasedOnChildren:
totalHeight += view.getMeasuredHeight() + 40;
but it adds some blank space for normal items that were true without adding 40.
What can I do to resolve this problem?

Listview height not correct

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

Android NestedScrollView showing only one Item in ListView

Anyone knows what's wrong in my layout? I'm not able to figure out if why is my ListView is showing only one item.
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:background="#color/grey_200"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.rostata.alejo.vbmobileapp.ActivityResult"
tools:showIn="#layout/activity_home">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/one">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="0dp"
android:background="#color/White"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginTop="20dp"
app:cardCornerRadius="4dp"
app:cardElevation="2dp">
<RelativeLayout
android:id="#+id/relativeGridInside"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:padding="10dp">
<RelativeLayout
android:id="#+id/headerThisNow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/border_bottom">
<Button
android:id="#+id/btnCloseWordOfTheDay"
style="?android:attr/buttonStyleSmall"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/ic_window_close"
android:text="" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fontFamily="sans-serif-light-bold"
android:text="Word of the Day"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/grey_700"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/headerThisNow">
<TextView
android:id="#+id/wordNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif-light-bold"
android:text="Paghurop-hurop"
android:textColor="#color/grey_700"
android:textSize="20sp" />
<TextView
android:id="#+id/thisPronounceNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/wordNow"
android:fontFamily="sans-serip-light"
android:text="-Pag-hu-rop-hu-rop-"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/grey_400"
android:textStyle="italic" />
<TextView
android:id="#+id/maningTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/thisPronounceNow"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:text="Meaning"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="#+id/thisMeaningNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/maningTitle"
android:ellipsize="end"
android:maxLines="2"
android:text="If ever na mabasa nindu ini, sample lang pu ni, maung comment please"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/thisMeaningNow"
android:layout_marginTop="10dp">
<Button
android:id="#+id/btnVoice"
style="?android:attr/buttonStyleSmall"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/btnMore"
android:background="#drawable/ic_voice"
android:padding="0dp"
android:text="" />
<ToggleButton
android:id="#+id/checkBox"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignTop="#+id/btnVoice"
android:layout_toEndOf="#+id/btnVoice"
android:layout_toRightOf="#+id/btnVoice"
android:layout_marginLeft="10dp"
android:background="#drawable/toogle_favorite_check"
android:text=""
android:textOff=""
android:textOn="" />
<Button
android:id="#+id/btnMore"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/btn_more"
android:text="More"
android:textColor="#color/White" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/card_view"
android:id="#+id/words_list_view"
android:divider="#color/transparent"
android:scrollbars="none"
android:layout_weight="1"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
I am using A Fragment Activity and SimpleCusrsorAdapter to populate an items to my ListView. in case of, this is my class;
public class ActivityHome extends Fragment {
CardView cardViewWordOfTheDay;
private WordsDbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
private String wordFromSpinner = "ALL";
ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.layout, container, false);
Button btnCloseWordOfTheDay = (Button) view.findViewById(R.id.btnCloseWordOfTheDay);
listView = (ListView) view.findViewById(R.id.words_list_view);
btnCloseWordOfTheDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cardViewWordOfTheDay = (CardView) view.findViewById(R.id.card_view);
cardViewWordOfTheDay.setVisibility(View.GONE);
}
});
dbHelper = new WordsDbAdapter(getActivity().getApplicationContext());
dbHelper.open();
displayListView(wordFromSpinner);
return view;
}
private void displayListView(String wordFromSpinnerData) {
Cursor cursor = null;
try {
cursor = dbHelper.fetchAllWords(wordFromSpinnerData);
} catch (Exception e) {
Toast.makeText(getActivity().getApplicationContext(), "EER " + e, Toast.LENGTH_LONG).show();
}
String[] columns = new String[]{
WordsDbAdapter.KEY_ONE, WordsDbAdapter.KEY_TWO,
WordsDbAdapter.KEY_THREE, WordsDbAdapter.KEY_FOUR, WordsDbAdapter.KEY_FIVE
};
int[] to = new int[]{
R.id.thisWord, R.id.thisSub, R.id.thisWordFrom, R.id.thisWordLocation, R.id.thisLetter
};
dataAdapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.words_list_item, cursor, columns, to, 0);
listView.setAdapter(dataAdapter);
}
}
I'm so confused. i dont what happen. when i try to change the height of my RelativeLayout to 500dp then the listView is showing the other items but not all. so i think i need it to be wrap_parent but this is not giving me the expected ouput.
Any help would be appreciated. Thank You Very Much!!!
As mentioned here,
You can call this method after listView.setAdapter(dataAdapter);
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, 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);
}

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

Categories

Resources