this is my layout for the same. my issue is, the list view in it is not increase the height with item count.
when I used wrap_content a given height like 210 pixel its shows the data... when I set height as wrap_content, it is showing only one item.
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/rl_createPro_option"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:visibility="gone"
android:layout_marginBottom="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please provide option-wise price:"/>
<ListView
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:id="#+id/rv_createPro"
android:layout_height="wrap_content" />
<RadioGroup
android:layout_width="match_parent"
android:id="#+id/rg_createPro"
android:layout_marginTop="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Option highlight??"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="yes"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="no"/>
</RadioGroup>
</LinearLayout>
please help me!!
This is because of your linearlayout with vertical orientation its keeping space for the lower views like radioGroups .Set height of listview dynamically use
public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
#Override
public void run() {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(
View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += listItem.getMeasuredHeight();
Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
}
Log.d("totalHeight " + totalHeight, "********");
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
listView.setLayoutParams(params);
listView.requestLayout();
}
});
}
I wanted to make my ListViews the size of the actual content. For example if there's 20 items I want it to display the 20 items without making the list itself scrollable like it is now.
It looks like this atm: The lists are very small and they are scrollable.
https://i.gyazo.com/eb8902a17936f54a2271b51abe228607.png
<ScrollView 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="com.example.tiagosilva.amob_android.ArchiveFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tube Data"
android:textColor="#color/AMOB_yellow"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tubeData_list">
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Tool Data"
android:textColor="#color/AMOB_yellow"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolData_list">
</ListView>
<Button
android:id="#+id/btn_clear_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_buttons"
android:text="Clear all"
android:textColor="#color/AMOB_gray"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"/>
</LinearLayout>
</ScrollView>
Try to use this, might be you gotta solution for it :-
Utility.setListViewHeightBasedOnChildren(yourlistview);
add this method :-
public static class Utility {
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);
}
}
It will not Scroll but u can get the size of the actual content as you want.
I am trying to implement listview inside scrollview or nestedscroll view but my list dose not visible last item properly .
please help me. how to visible all item properly
here is my code
<LinearLayout 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:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/electronic_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#string/no_data_find"
android:textSize="40dp"
android:textStyle="bold"
android:visibility="gone" />
<RelativeLayout
android:id="#+id/rltop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:background="#color/white">
<TextView
android:id="#+id/tvadver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="ADVERTISEMENT"
android:textColor="#color/light_grey"
android:textSize="12dp"
android:textStyle="normal" />
<RelativeLayout
android:id="#+id/rl_viewfliper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvadver"
android:layout_marginBottom="10dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="10dp">
<ViewFlipper
android:id="#+id/view_flipper"
android:layout_width="fill_parent"
android:layout_height="130dp">
</ViewFlipper>
</RelativeLayout>
</RelativeLayout>
<ListView
android:id="#+id/fashionLV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvmsg"
android:layout_weight="1"
android:divider="#android:color/transparent"></ListView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
Java code
commanAdapter = new CommanAdapter(getActivity(), commanModelArrayList);
fashionLV.setAdapter(commanAdapter);
GeneralFunction.setListViewHeightBasedOnChildren(fashionLV);
GeneralFunction code:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ArrayAdapter listAdapter = (ArrayAdapter) 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);
}
The use of "ListView" and "ScrollView" objects together is not recommended.
You should use different views in same adapter. For example;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
MyListItem currentItem = myList.get(position);
LayoutInflater Inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (currentItem.getListItemType() == "Description") {
rowView = Inflater.inflate(R.layout.descriptionlayout, null);
TextView tvDesc= (TextView) rowView.findViewById(R.id.tvAccountType);
tvDesc.setText("Desc");
} else if (currentAccountItem.getListItemType() == "ListItem") {
rowView = Inflater.inflate(R.layout.list_item_layout, null);
...
}
}
You should add "ListItemType" property to your list object type. And check this property in getView() method and inflate different layouts.
In your case, you can create new layout for your LinearLayout. And insert an object to your list for this row. And set ListItemType. and check this property and inflate this layout on getView().
Hope it helps.
You add your own footer view in your listview by using list view methods.
It will shows after all the elements of listView.
ListView inside NestedScrollView creates lot of problems and ListView is not recommended to use instead go for RecyclerView, you have to change the Adapter accordingly.
I want a Fragment that has 2 ListView. I dont want the listView to scroll instead i just want the whole Fragment to scroll. Heres the code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<TextView
android:id="#+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:paddingTop="15dp"
android:text="Monday"
android:textColor="#000"
android:textSize="55sp" />
<TextView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/day"
android:gravity="center_horizontal"
android:text="27 April, 2014"
android:textColor="#000"
android:textSize="45sp" />
<ListView
android:id="#+id/home_list_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/date"
android:layout_margin="10dp"
android:background="#color/list_home_time_bg"
android:divider="#android:color/white"
android:dividerHeight="20.0sp" />
<ListView
android:id="#+id/home_list_stime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/home_list_time"
android:layout_margin="10dp"
android:background="#color/list_home_stime_bg"
android:divider="#android:color/white"
android:dividerHeight="20.0sp" />
</RelativeLayout>
When i run this code the 2nd listView gets its own scroll bar, is there a way to stop this and make the entire Fragment to scroll?
To stop the listview scrolling use -
listView.setScrollContainer(false);
To scroll fragment put it inside ScrollView -
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- everything you already have -->
</ScrollView>
For more check - how-to-get-a-non-scrollable-listview.
NOTE:
If you put ListView inside a ScrollView then all the ListView does not stretch to its full height. Below is a method to fix this issue.
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
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();
}
To use this method just pass the ListView inside this method :
ListView list1 = (ListView) view.findViewById(R.id.home_list_time);
setListViewHeightBasedOnChildren(list1);
ListView list2 = (ListView) view.findViewById(R.id.home_list_stime);
setListViewHeightBasedOnChildren(list2);
More here - android-list-view-inside-a-scroll-view.
In my app i want to make a page that have ExpandableListView and below him CheckBox.
My problem is that my ExpandableListView is too big and make the CheckBox out of the page.
On smaller screens is not visible at all.
I tried to put ExpandableListView inside scrollview but its not working.
Is there a way to make my design?
Here is my code and a screenshot.
XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15sp"
android:text="In this page you can save a group of exercise under one routine."
android:textColor="#color/Black"
android:textSize="20sp" />
<ExpandableListView
android:id="#+id/instructionsExView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="15sp" >
</ExpandableListView>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/instructionsExView"
android:layout_marginTop="15sp"
android:text="If you want to open the instruction again check 'need help?'"
android:textColor="#color/Black"
android:textSize="20sp" />
<CheckBox
android:id="#+id/checkInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="16dp"
android:checked="false"
android:text="dont show again when opening the page"
android:textColor="#color/Black" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Screen shot:
EDIT:
in your "onCreate" method write: "setListViewHeight(expListView)" after "expListView.setAdapter(listAdapter)" then set OnGroupClickListener for your expandable list view as below:
expListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
setListViewHeight(parent, groupPosition);
return false;
}
});
used methods:
private void setListViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
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();
}
private void setListViewHeight(ExpandableListView listView,
int group) {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.EXACTLY);
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupItem = listAdapter.getGroupView(i, false, null, listView);
groupItem.measure(desiredWidth, 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, 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();
}
now u are good to go.
Based on #Dmila Ram`s answer, I was able to do it.
In my case, I initialize and populate the ExpandableListView in the onCreate method like so:
expandableListView = (ExpandableListView) findViewById(R.id.appsMenu);
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expandableListView.setAdapter(listAdapter);
for (int i = 0; i < listAdapter.getGroupCount(); i++)
expandableListView.expandGroup(i);
setListViewHeight(expandableListView);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
setListViewHeight(parent, groupPosition);
return false;
}
});
Notice that I expand each group. After that I call setListViewHeight(expandableListView);
This method is responsible for calculating the height of the ExpandableListView when it is created for first time.
Here is the code:
private void setListViewHeight(ExpandableListView listView) {
ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getGroupCount(); i++) {
View groupView = listAdapter.getGroupView(i, true, null, listView);
groupView.measure(0, View.MeasureSpec.UNSPECIFIED);
totalHeight += groupView.getMeasuredHeight();
if (listView.isGroupExpanded(i)){
for(int j = 0; j < listAdapter.getChildrenCount(i); j++){
View listItem = listAdapter.getChildView(i, j, false, null, listView);
listItem.measure(0, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
}
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
It's really not necessary to have the groups expanded, instead we can just loop through the child items and add them to the calculation of the total height.
This will make all widgets in the ScrollView scrollable and it will also show the ExpandableListView properly.
Then we also need this method, to make sure that clicking on the groups will calculate the height as well:
private void setListViewHeight(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();
}
That's pretty much it, then it works fine, but this solution can be optimized even further.
you cant use listview inside scroll view ,so you should use scroll view and add the rows as views dynamically.
You do not need more than one RelativeLayout, because expandablelistview should be between two items (in your case), these two items are going to be the first ones with parentTop and parentBottom, the rest will be positioned playing with above/below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15sp"
android:text="In this page you can save a group of exercise under one routine."
android:textColor="#color/Black"
android:textSize="20sp" />
<CheckBox
android:id="#+id/checkInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="16dp"
android:checked="false"
android:text="dont show again when opening the page"
android:textColor="#color/Black" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/checkInstructions"
android:layout_marginTop="15sp"
android:text="If you want to open the instruction again check 'need help?'"
android:textColor="#color/Black"
android:textSize="20sp" />
<ExpandableListView
android:id="#+id/instructionsExView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_below="#id/textView1"
android:layout_marginTop="15sp" >
</ExpandableListView>
</RelativeLayout>
You can use below code to achieve this thing. I used ListViewyou can replace with ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15sp"
android:text="In this page you can save a group of exercise under one routine."
android:textColor="#color/WHITE"
android:textSize="20sp" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2" >
</ListView>
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp"
android:text="If you want to open the instruction again check 'need help?'"
android:textColor="#color/WHITE"
android:textSize="20sp" />
<CheckBox
android:id="#+id/checkInstructions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginBottom="20dp"
android:layout_marginTop="16dp"
android:checked="false"
android:text="dont show again when opening the page"
android:textColor="#color/WHITE" />
I checked the above code with around 50 listitem value and tested on a very small display device and it works fine.
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15sp"
android:text="In this page you can save a group of exercise under one routine."
android:textColor="#color/WHITE"
android:textSize="20sp" />
<ExpandableListView
android:id="#+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2" >
</ExpandableListView>
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp"
android:text="If you want to open the instruction again check 'need help?'"
android:textColor="#color/WHITE"
android:textSize="20sp" />
<CheckBox
android:id="#+id/checkInstructions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginBottom="20dp"
android:layout_marginTop="16dp"
android:checked="false"
android:text="dont show again when opening the page"
android:textColor="#color/WHITE" />
I checked with ExpandableListView and it working fine for me.