<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="600dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<include
android:id="#+id/include1"
android:layout_width="fill_parent"
android:layout_height="70dp"
layout="#layout/header_history" >
</include>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#android:color/black"
android:dividerHeight="1.0sp"
android:textColor="#000000" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
header_history
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/button" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/button"
android:gravity="left|center_vertical"
android:text=" History"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
I am trying to make my list view scrollable but it has become scrollable but I am unable to find the details of the list view. Please help me for the same. Friends,I have updated the header_history file also.
I've answered a question similar to this only. Here i explain you about why its not scrolling?
Simply, remove the ScrollView from your layout. And, make the LinearLayout as parent. And, try to run your app. It will scroll the list.
Because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView I strongly recommend you to simplify your layout somehow. For example you can add views you want to be scrolled to the ListView as headers or footers.
Have a look at this
See my existing answer
The problem is that you put a ListView in a ScrollView. The Android API says you should never ever put a scrollable element in another scrollable element.
If you need to to this anyway, you can solve the problem by subclassing the ListView and overwriting the onMeasure() method.
How can I put a ListView into a ScrollView without it collapsing?
http://nex-otaku-en.blogspot.de/2010/12/android-put-listview-in-scrollview.html
Edit: You use the following code to create a fully expanded list. Also it's worth to notice, that this might be a horrible idea for large list, due to performance reasons.
public class FullExpandedListView extends ListView {
public FullExpandedListView(Context context) {
super(context);
}
#Override
protected void onMeasure(int a, int b) {
super.onMeasure(a, b);
setListViewHeightBasedOnChildren();
}
private void setListViewHeightBasedOnChildren() {
SwfListAdapter listAdapter = (SwfListAdapter) getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listControl);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listControl.getLayoutParams();
params.height = totalHeight + (listControl.getDividerHeight() * (listAdapter.getCount() - 1));
setLayoutParams(params);
}
}
Related
I got a fragment in my tablayout displaying a listview:
<android.support.design.widget.CoordinatorLayout 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"
android:background="#color/backgroundColor"
android:fitsSystemWindows="true"
tools:context=".activities.fragments.OrdersFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/orderListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
This Listview has items which have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/order_list_order"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/arrowView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_weight="1"
app:srcCompat="#drawable/ic_downarrow" />
<LinearLayout
android:id="#+id/previewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/order_list_storename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Store Name"
android:textColor="#color/generalText"
android:textSize="24sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
some textview here
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
some textview here
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/extensionLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/previewLayout"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Produkte:"
android:textColor="#color/generalText"
android:textSize="18sp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/exampleArray"/>
</LinearLayout>
</RelativeLayout>
These items all have also a listview which has the exampleArray as items:
<string-array name="exampleArray">
<item>Donut</item>
<item>Pizza</item>
<item>blabla</item>
</string-array>
My Problem is, that only the first element of this inner listview is shown. Also the outer Listview is not clickable anymore. Does anyone know why this is the case? Am I doing anything wrong?
Its not a good idea to put a scrollable view inside another scrollable view. Try to solve your problem using getItemViewType method.
If you don't have any other option, then you have to measure inner ListView height. This way all item will be inflated at the same time, you can't take advantage of recycle property of ListView.
To measure inner ListView use this method
/**** 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();
}
Usage:
setListViewHeightBasedOnChildren(listView)
This way you inner LisView will lose recycle property.
There is other questions about this, You can check those too,
Check this and this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">//changed here
<ListView
android:id="#+id/orderListView"
android:layout_width="match_parent"
android:layout_height="match_parent"//and here
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px" />
</RelativeLayout>
Please use 'match_parent' with 'ListView' else use RecyclerView
I have a android layout like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/headingView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:paddingTop="#dimen/value_5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:visibility="gone" />
<LinearLayout
android:id="#+id/list_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/mt_gray_color"
android:fadeScrollbars="false" >
<TextView
android:id="#+id/itemHeading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textStyle="bold"
android:background="#f2f2f2"
android:layout_marginTop="15dp"
android:visibility="gone"/>
<ListView
android:id="#+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="#dimen/value_5dp"
android:minHeight="500dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
I am loading data dynamically in ListView after this page/fragment has been loaded.
The listview contains more than one element and I was expecting that it will show me list in the listview without scrolls as I have made it WRAP_CONTENT.
I might have wrong understanding and which what I want to understand and find a solution that how can i avoid the scroll bars and have all the items listed in the listview without scrolls.
Thanks in advance.
You could calculate the size off all children of the ListView at runtime and then set it's height to the total height of all the children combined. You can use this method:
public void justifyListView(ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}
You can change the height of listView when the data changed.
I'm trying to create a layout that contains a ListView with a button underneath it, and also make it so that the button 'sticks' to the bottom of the ListView, even when I add or delete a row from the ListView.
With the layout code below, when I add a new row to the list, the button 'moves' to its correct location right beneath the bottom of the ListView. So that works fine.
The problem is when I delete a row from the ListView, the button stays where it is and doesn't 'move up' so that it sticks to the bottom of the ListView. When I rotate the device and it recreates the view, the button does in fact move up, but I'd like it to automatically move up when a row is deleted.
Here is the code I have now:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.0" />
<ImageButton
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_button" />
</LinearLayout>
SOLUTION:
The solution was to create a small layout file that contains just the button, then add it as the footer of the ListView programatically.
Inside my fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
...
View footerView = inflater.inflate(R.layout.listview_footer, null, false);
addButton = (ImageButton) footerView.findViewById(R.id.addButton);
listView.addFooterView(footerView);
data = getListViewData();
adapter = new MyListAdapter(getActivity(), data);
listView.setAdapter(adapter);
...
}
listview_footer.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_button" />
The listview_footer.xml just contains the button, and no layout.
Now, when I delete a row from the ListView, the button moves up to 'stick' to the bottom of the ListView. And as before, when I add a row to the ListView, the button moves down below it.
You can add button to the footer of the listview.
View footerView = View.inflate(this, R.layout.footer, null);
listview.addFooterView(footerView);
Make an xml file for the footer view in which you will add a button as per your UI needs.
Footer.xml may look like following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="5dip">
<Button android:id="#+id/previous"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
This link may be helpful for you.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageButton
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below= "#+id/listView"
android:src="#drawable/add_button" />
</RelativeLayout>
I had a similar situation where I wanted a button to always be in the bottom right of my list, and there would be many deletion/additions to the list. I used a relative Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/search_bg" >
<ListView
android:id="#+id/list_searchfilters"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#android:color/transparent"
android:dividerHeight="10dp"
android:listSelector="#drawable/search_list_selector" />
<Button
android:id="#+id/add_filter_button"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_below="#id/list_searchfilters"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp" />
</RelativeLayout>
And everytime a row would be deleted or added I would just call LayoutUtils.setListViewHeightBasedOnChildren(mSearchListView); which I had defined as :
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();
}
User RelativeLayout instead. Add android:layout_alignParentBottom="true" to Image Button and android:layout_above="#+id/addButton" to listView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/addButton"
android:layout_alignParentTop="true"/>
<ImageButton
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_button"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
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.
I'm trying to make an activity with a static header and a scrollable dynamic list. Here is the Layout:
<GridLayout
android:id="#+id/GridLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ListView
android:id="#+id/listex"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
I tried to use the height=0dp & weight=1 solution but the result is that when I add something to the list, it does not show any element.
If I use height= match_parent & weight=1 the elements are shown but is not scrollable
Please help, I tried a lot of combinations gettign no results.
Thanks.
I have been using this list view and it works fine.
Maybe you don't have enough items in you list view, for it to scroll?
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="290dp"
android:layout_weight="0.50" >
</ListView>
You can try using below codes :
<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"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="static header" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
This is my solution.
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:fadeScrollbars="true"
android:layout_weight="1" />
set ListView weight to 1. or You can do like this.
just after u set ur adapter call this method and set adapternotifydatasetchange() after dat.
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, ViewGroup.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);
listView.requestLayout();
}
your list in the GridLayout remove the parent of list view that is GridLayout it will scroll perfectly.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ListView
android:id="#+id/listex"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
The vertically scrolling GridLayout should not contain another vertically scrolling widget (ListView).You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent that is its ScrollView
Set to the linearLayout weightSum = 1