Prevent ListView from scrolling - android

I have a scrollview with a linear layout in it. Within that linear layout there is a list view. The problem I am having is that my list view only has a certain amount of space and then becomes scrollable. I don't want this to happen. I want the entire list to be shown. Does anyone know how I might achieve this? I have tried using this link however my layout does not appear when I add the footer. ListView in ScrollView potential workaround This is my code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="true"
android:orientation="vertical"
android:background="#drawable/darkimg"
android:weightSum="1">
<LinearLayout
android:id="#+id/numbersLL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/round_rectangle"
android:layout_margin="4px"
android:paddingLeft="6px"
android:paddingTop="3px">
<TextView
android:id="#+id/numberPrint"
android:layout_height="wrap_content"
android:textSize="14px"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_width="fill_parent"
android:text="#string/numberPrint">
</TextView>
<ListView
android:id="#+id/numberListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
This is my footer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/formLL">
<TextView
android:id="#+id/numberFormTextViewt"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20px"
android:text="Add a number:"
android:textColor="#color/white" />
<Button
android:id="#+id/numberFormButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_add"></Button>
</LinearLayout>
And this is the Java code to add the footer to the list:
LayoutInflater factory = getLayoutInflater();
LinearLayout numberFooter = (LinearLayout) factory.inflate(R.layout.number_form, null);
// List of numbers
numberListView = (ListView) this.findViewById(R.id.numberListView);
numberListView.addFooterView(numberFooter);
What is going wrong here? The footer does not appear when the activity is displayed. One thing I should mention is the footer is used to add items to the list above it. I'm not sure if that makes a difference but when I am testing this the list adapter is empty. Even still should the footer show up anyway? Any help would be appreciated thanks.

List view under scroll view is not working(means List view is not scrollable).
So u can put empty Linear Layout instead of List view in ur xml file. In ur code, first u can get the list view size, based on this size value, for loop can rotated. In that for loop u can call the get view method of the adapter class(that class is not extending any class.) this will solve the list view problem.

try this in your listview:
android:layout_height="fill_parent"

Related

Android Widget lost from view if the list is to long

I have the following layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:longClickable="true"/>
<TextView
android:id="#+id/notes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
The TextView is populated programmatically and is shown at the bottom of the list as I require however when the list starts to fill the screen the TextView is list behind the list and can no longer be seen. How can I stop the List view expanding into the space occupied by the text view?
I was thinking about changing it to a ScrollView but is was my understanding that you shouldn't use a ListView inside a ScrollView.
Use a RelativeLayout Instead Of LinearLayout
and add this to your ListView
<ListView
...
android:layout_above="#+id/notes"
/>
if you want the textView To be at the bottom of your view add this too:
android:layout_alignParentBottom="true"

How to make a list of textviews?

Basically here is a mspaint demonstraiton of what I want to accomplish:
I do not want the outline to be visible to the user, it was just to give you an idea of what I want to do. I want new textviews to take position under each other whenever a new goal is added from the edittext-button combo on the bottom part of the GUI.
I know how to save the data - SQLite. But I have no idea how to make new textviews be on the bottom of the last one - and if there is no more room on the screen I want that textview part to be scrollable.
I don't know what I need to use to accomplish this.
Thanks in advance!
create two xml files.
One consisting of list view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/bkgrnd_320x285">
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="1dp"
android:fastScrollEnabled="true"/>
</LinearLayout>
and another xml for just the textView eg. row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Name"
android:textSize="20dp"
android:layout_marginTop="20dp" />
</LinearLayout>
Then in the main activity u need to refer to both the xml files as follows
ListView ll = (ListView) findViewById(R.id.List1);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.row,R.id.textView2, arraylist_you_want_to_display);
ll.setAdapter(arrayAdapter);
where R.layout.row is the row.xml consisting of textView
And R.id.textView2 is the id of the textView present in the row.xml
You may create a LinearLayout, setting the orientation to vertical.
Adding a ScrollView or ListView to show the upper part, with android:weight=1
Add the EditText to the LinearLayout, and this is done.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/List1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weight="1"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

scrollview is not working

I got a list in my main but problem is that when I scan the barcodes for more than 15 barcodes, then it didn't show the"scan" button already and I tried to use the scroll view for that but it is not working :
Any helps?
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="no notes"
/>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="scan"/>
</LinearLayout>
</ScrollView>
Any suggestions?
Generally it is a bad idea to place a listview inside of the scrollview. It doesn't work as expected. See the following post:
https://groups.google.com/forum/?fromgroups#!topic/android-beginners/LRpLgGOy2Pc
In Android it's not possible to use a ListView in a ScrollView. In order to achieve the same result you can use a ListView and add a header and a footer.
addHeaderView()
You can use a ListActivity. Create one xml layout file for your header and add it to the ListView: (put the Code in your onCreate() function before calling setAdapter())
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
getListView().addHeaderView(inflater.inflate(R.layout.header, null));
getListView().addFooterView(inflater.inflate(R.layout.footer, null));
The footer works exactly the same way.
Hi, you can use RelativeLayout instead of LinearLayout.
In such case you should place your button at the bottom of the layout. After that other controls should be placed above the button. RelativeLayout has special xml-attributes to help you do this. After that you do not need a ScrollView.

ListView pushes other Views off the screen

I'm struggling to get a layout looking correctly, and I've tried to produce the shortest, smallest possible example of my problem.
My goal is to have a header and footer View, at the top and bottom of the screen, with a ListView in between the two, with another View (let's call it the label, it's the gray box from the screen shots) directly below the ListView. This label, and the footer should always be shown when ListView needs to scroll.
Visual Result
When the ListView does not need to scroll (this is correct):
When the ListView needs to scroll, the footer and the gray box are pushed off screen (wrong):
Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="#color/red"/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="#color/light_gray"
android:textColor="#color/black"/>
<!-- Used to push the footer to the bottom -->
<View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="#color/blue"/>
</LinearLayout>
Test Activity
public class TestActivity extends ListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<String> items = new ArrayList<String>();
items.add("one");
items.add("two");
items.add("three");
items.add("four");
items.add("five");
items.add("six");
items.add("seven");
items.add("eight");
items.add("nine");
items.add("ten");
setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1, items));
setContentView(com.myproject.android.R.layout.test);
}
}
I've tried a few different approaches, such as giving the ListView layout_weight="1" and removing the blank View that I use to push the footer to the bottom. This is almost what I want, it keeps the footer and label visible when the ListView scrolls, but when it only has 1 or 2 items, I need the gray box right below the ListView. I've also attempted to use a RelativeLayout, without success. I guess I'm completely misunderstanding things.
EDIT
Here's my attempt with a RelativeLayout which still isn't correct.
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="#color/red"
android:id="#+id/header"
/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_below="#id/header"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="#color/light_gray"
android:textColor="#color/black"
android:layout_below="#android:id/list"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="#color/blue"
android:layout_alignParentBottom="true"
android:id="#+id/footer"/>
</RelativeLayout>
Relative Layout (Still Wrong):
Add android:layout_weight="1" to the listview. That will make it the biggest element in the LinearLayout, without pushing the other ones off the screen.
This layout adds the header a top of the screen and the footter and the bottom. The list fills the rest of the screen. With theses aproach list elements never be obscured by the footer. See how to add the gray box below the XML...
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="#color/red"
android:id="#+id/header"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="#color/blue"
android:layout_alignParentBottom="true"
android:id="#+id/footer"/>
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/list"
android:layout_below="#id/header"
android:layout_above="#id/footer"/>
</RelativeLayout>
Ok. This XML solves the problem of the missing footer. Now we have to add a gray box at the end of the list. I think there is two ways to do it:
Use the addFooterView method: http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)
Play with the adapter and the getViewTypeCount() method so you can define two types or elements: normal elements and footer element. http://developer.android.com/reference/android/widget/BaseAdapter.html#getViewTypeCount()
A solution that worked for me was to add positive padding to the bottom of the list view and negative padding to the top of the "footer". This will work in a linear layout or a relative layout.
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-50dp"/>
Two years late to answer the question, but I will leave my solution so it may help someone with the same problem. I solve this problem using 2 nested LinearLayouts and using layout_weigth. Maybe not the best performatic layout, but it reaches the desired effect.
You need to arrange your layout this way:
Your ListView will have wrap_content height to take only the needed space when not filling the entire screen.
Your ListView will be inside a layout with height using layout_weight so the list will take only the needed space when not filling the entire screen and to take only a limited space of the screen when it have size enouth to push the views out of screen.
The grey box view the should be immediately below the list will have wrap_content height and will be a sinbling of the layout of step 2.
This layout and the grey box will be inside a second layout with wrap_content height so they can stay together.
Now you have a layout with the list and the grey view and the list won't push the other views out of screen if it gets too big; you only need to move the footer view to the bottom of the screen.
5a. If you are using RelativeLayout as your root layout, you can do as sgallego said and use android:layout_alignParentBottom.
5b. But if you are using LinearLayout you need to create a third layout with layout_weigth and put inside the layout of step 4 and a empty view also with layout_weigth to fill the empty space.
Here is a commented example.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Header -->
<TextView
android:id="#+id/RecordStudy_StudyLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/RecordStudy_StudyLabel"
android:textSize="#dimen/text_large" />
<!-- Body -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<!--
This layout encapsules the list and the button that must be immediately
below the list with a wrap_content height, so the list plus the button
fills only as much space as they need (if the list is not big enouth to
fill the entire screen).
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!--
Layout with varaible size with a list inside.
Using layout_weight tells android that this layout should not grow
greater then the screen, but uses only the free space.
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<!--
Inside this limited height layout, there is a list with height
wrap_content so it can grow as much as it needs INSIDE the
layout (through scrolling).
-->
<ListView
android:id="#+id/RecordStudy_StudyList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- Button immediately below the list -->
<Button
android:id="#+id/RecordStudy_AddStudy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/RecordStudy_AddStudy" />
</LinearLayout>
<!-- Space between the list and the footer -->
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<!-- Footer -->
<Button
android:id="#+id/RecordStudy_ConfirmButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/RecordStudy_ConfirmButton" />
</LinearLayout>
One solution that I implemented and found useful was to keep the listview inside a linear layout with fixed height so that it doesn't extend and overlap other items.
Something like this:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="#color/red"
android:id="#+id/header"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dip" //assume 150dip height is sufficient
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_below="#id/header"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="#color/light_gray"
android:textColor="#color/black"
android:layout_below="#android:id/list"/>

how to put a button at the end of ExpandableListview dynamically in android

In my application i have an activity which extends ExpandableListActivity, there i want to put a button at the end of ExpandableListview, so that if i exapand it should be at the end of listview.
The problem is after exapanding all the items in Expandablelistview it takes the full screen and the button does not appear.
How can I scroll button with ExpandableListview?
I also tried to give Scrollview but its not working.
Give me a way what should I write in xml file for proper o/p.
Thank you
Create a footerview layout consisting of Button that you want to set as footer and then try
View footerView = ((LayoutInflater)ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);
Footer_layout xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<Button
android:id="#+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip">
</Button>
</LinearLayout>
</LinearLayout>

Categories

Resources