Set ListView Height programmatically in Android - 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?

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

listview dynamic items height

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

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

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

Categories

Resources