Limit number of rows of listview - android

I am developing an application that needs to pair with other devices through Bluetooth. I was having trouble in order to show the list of paired devices in correct manner. I had also viewed the example in the android api (Bluetooth Chat), but i was having the same problem.
The list of paired devices are to big and then hides a search button that are at the bottom of the list.
My xml code is very the same of the example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="#+id/listpaired_devices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/title_paired_devices"
android:visibility="gone"
android:background="#666"
android:textColor="#fff"
android:paddingLeft="5dp"
/>
<ListView android:id="#+id/paired_devices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="1"
/>
<TextView android:id="#+id/list_new_devices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/title_other_devices"
android:visibility="gone"
/>
<ListView android:id="#+id/new_devices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stackFromBottom="true"
android:layout_weight="2"
/>
<Button android:id="#+id/btscan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btscan"
/>
</LinearLayout>
But and i can't show the search button at the bottom.
Here my screen:
My Screen
You could see a bit of the button at the bottom of the dialog window.
It's possible to limit the number of rows shown at the listview ? Can anyone tell me how i can fix this problem

Firstly some points about your code:
layout_weight is only meaningful if an object has no size in a certain dimension, that is you set layout_height or layout_width to 0, so this has no effect on your code.
Setting the height of a ListView to wrap_content is pretty meaningless, and even if it works it's bad practice. Use either 'fill_parent' or a definite height.
The button is hidden because, as per the point above, the ListViews you have created have no predefined size so take up as much space as they can, pushing the button off the screen.
So let's think about what you really have there - it's just a single list with a button at the bottom (yes you may have headers or multiple sources of data in there but we'll get onto that).
The following layout will give you a ListView at the top and a Button at the bottom. The ListView will take up any space not being used by the Button. Notice how the Button is defined before the ListView in the layout - this causes Android to calculate how much height the button takes up before considering the ListView. It then gives the ListView the rest of the height available.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="#+id/btscan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="#string/btscan"
/>
<ListView android:id="#+id/all_devices"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/btscan"
android:stackFromBottom="true"
/>
</RelativeLayout>
So that's the layout. Now lets consider the actual content of your list: you have a header, followed by a list of paired devices, followed by another header and then a list of new devices.
You can create this using a single Adapter - Commonsware provides a very good implementation called MergeAdapter but you could code your own. MergeAdapter doesn't directly let you a view (e.g. for the headers) but thankfully Commonsware also provides the SackOfViewsAdapter which allows you to add views to it, and then you can add the SackOfViewsAdapter to the MergeAdapter.
Here is some pseudo-code which should accomplish what is outlined above.
// This is the adapter we will use for our list with ListView.setAdapter
MergeAdapter listAdapter = new MergeAdapter();
// The first header - you'll need to inflate the actual View (myHeaderView1) from some resource
// using LayoutInflater
List<View> firstHeaderViews = new List<View();
firstHeaderViews.add(myHeaderView1);
SackOfViewsAdapter firstHeaderAdapter = new SackOfViewsAdapter(firstHeaderViews)
// Second header
List<View> secondHeaderViews = new List<View();
secondHeaderViews.add(myHeaderView2);
SackOfViewsAdapter secondHeaderAdapter = new SackOfViewsAdapter(secondHeaderViews);
// Now to add everything to the MergeAdapter
// First goes the first header
listAdapter.addAdapter(firstHeaderAdapter);
// Now your first list
listAdapter.addAdapter(firstListAdapter);
// Now your second header
listAdapter.addAdapter(secondHeaderAdapter);
// Now your second list
listAdapter.addAdapter(secondListAdapter);
// Now set the adapter to the list
myList.setAdapter(listAdapter)
The layout produced should look something like this. Note I extended the list to show how it behaves with a list longer than the screen; the button still remains visible. The red box marks the bounds of the screen.

You can limit the number of rows shown at the list view, but not sure if that will really help you in what you want to achieve, because you're hiding information from the user that might be important to him. You can limit the number of rows by limiting the number of items you pass to the listview adapter, or you can set the visibility to 'gone' in the getView method when the list view reaches a certain position (you can check the 'position' parameter of getView()).
However, I would suggest you use only one list view (add a separator for the 'new/other devices' title into the view for a list item, but hide it by default, and then, as already suggested by suri, use headers and footers for the listview (to place the scan button).

Related

can i have a listview and an expandablelistview in the same view

I have several edittexts in my view for taking input for a database query .I'm displaying the query results in a list view below the edittextfields. Because there are 7 edittextfields there is not enough space for the listview. I wanted to put all the edittexts in an expandablelistview.
Problem i'm facing is -only the expandablelistview item is shown -the listview doesnt come up at all --i have #android:id/empty and #android:id/list entries ---but nothing comes up
i can't add the listview into the expandablelistview as that is not allowed + since i need both #android:id/empty and #android:id/list to show query results i need a proper list view.
what are my options --? i could ofcourse display the results in a separate window
If you could show the xml you have right now it would be great but the ideas behind it needs to be something like:
<?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="wrap_content"
android:orientation="vertical" >
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Edit:
Okay so i figured out that for this layout to function correctly you need to set dome weight so that the linearlayout understand what size to give each children. Use:
android:layout_weight="1"
in the listview and the expandablelistview (1 in both means thay will divide space between them)

How to place fixed button below listview?

Is there any way to have a button directly below a listview, so that as the listview grows, the button moves down BUT the button is never pushed off screen. IE, once the listview has outgrown the screen, the button is still always visible, and the listview is scrollable.
I have managed to make the button ALWAYS at the bottom of the screen, but i want it to sit up directly below the listview while the listview is smaller than the screen.
I have tried using various arrangements of relative and linear layouts and using the weight property, and things that seem like they should work simply don't, so it might be worth checking any answers before posting.
CLARIFICATION:
To phrase it in a different way: I want a button to sit below a listview, moving down as it grows, but i dont want the button to be pushed offscreen
This previous post does exactly what you want to do. What it does basically is that it keeps the button at the bottom of the list at all times. But when the list grows out of the screen area, its height gets limited by the weight parameter.
This way, the list's bottom edge is just above the button's LinearLayout and you get the same behavior that you were looking for.
If You Want to show this button in the end of list item. Then use this code
final Button btnAddMore = new Button(this);
btnAddMore.setText(R.string.art_btn_moreIssues);
exArticlesList = (ExpandableListView) this.findViewById(R.id.art_list_exlist);
exArticlesList.addFooterView(btnAddMore);
OR If you show button in your layout end then use this code.
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/btn_New" >
</ListView>
<Button
android:id="#+id/btn_New"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:text="#string/New"
android:width="170dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Scrolling up and down the ListView loses the top and bottom items in the list - Screenshot attached

I have an activity with the following items:
Action bar (one imageview, one view and two imagebutton) displayed on top of the activity, and for the rest of the screen I have one ListView. This is all placed in main.xml layout. The list will always have 6 rows no more and the action bar is not visible on this activity.
The listview loses text/images from some rows when scrolling up and down.
Before scrolling (I had to hide the logo though, sorry)
Before scrolling http://img706.imageshack.us/img706/37/32572081.png
After scrolling down and up (note DGL Lookup text disappear)
after first scrolling http://img638.imageshack.us/img638/7334/4scrollup.png
After scrolling down and up again (note DGL Lookup is still not there and Hazard Classes graphic and text disappear too
scrolling again and again http://img839.imageshack.us/img839/4970/5scrolldownagain.png
If I keep scrolling up and down, rows might show up again and might disappear as well.
here is my main.xml
<?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:background="#FFF"
android:orientation="vertical" >
<include layout="#layout/action_bar" />
<ImageView
android:id="#+id/splashscreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
</ImageView>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:textColor="#000" >
</ListView>
</LinearLayout>
Any quick help! I am very new to Android
#Frankenstein: Thanks for your help, you gave me the thread head and I followed it until the I got the solution.
The issue was that the listview display certain number of rows on a screen, and display more rows as you scroll up and down using getView method. The new rows displayed will normally take the style of the rows they are replacing.
So, if row1 has a different font style than others, then the row that will take it's place will inherit the same font style. What you may do, check if this is a different row that shouldn't inherit the style and then change the style to what you like.
Check this article http://android.amberfog.com/?p=296 which will explain what I just said in a technical way

list inside scrollview

On my screen I have a list view and a button. my list has like 8 item. I would like my screen to scroll if both these items does not fit in. I don't want my list to have scroll but the complete layout including both list & button. If I use the below layout it only shows on item inside the list and I have to scroll within the list to go to next item.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#android:id/list"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#drawable/round_background" />
<Button android:text="Search" android:id="#+id/carSearchButton"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
You can't put a ListView inside a ScrollView. Of GridView, or whatever View that handles scrolling on the same axis as the ScrollView does. That way the framework wouldn't know which View should handle the scrolling event. This layout won't produce an error when you compile it, but it won't work properly.
What you should do here: dump the outer ScrollView, you don't need it. Only use a ListView, and add the button to the ListView, using .addFooter(), that's the easiest way. This way your button'll appear as a list element, but you don't have to mess around with a custom adapter.
Scythe kind of answers my question but I wanted more then one one control below the list also on another screen I wanted 2 lists. So in order to have the scroll bar working with list view I had to fix the height of the list.

Adding Empty Space to End of ListView

I have a ListView that with alphabetical headers for each letter. I also have an index function that brings the letter headers to the top of the screen.
My problem is when I reach the end of the list setSelection is unable to bring the last few headers to the top because it will not scroll past the end of the list.
My question is this: Is there a way to add a blank space to the end of the screen dependent on screen size? I would like to scroll until the last item in the list is at the top of the listView.
The easiest way to add space is to add padding in xml and set clipToPadding:"false".
For RecyclerView
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"/>
For ListView
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"/>
And same goes for the ScrollView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"/>
This specifically adds the blank space to top and bottom but hide the space when you scroll the view.
Try the followings:
View footer = new View(getActivity());
footer.setLayoutParams( new AbsListView.LayoutParams( LayoutParams.FILL_PARENT, 100 ));
mListView.addFooterView(footer, null, false);
I'm assuming you are using an extension of BaseAdapter to populate your ListView?
There may be a built-in way to do what you are asking, but I don't know of one. If you end up creating it yourself, how about this approach:
Return list.size() + EXTRA in getCount()
Modify getItem() to return something sane if it asks for an item not in your list
Modify getView() to configure the given view as a simple horizontal padding with the same height as the rest of your views if the position index is more than your list size
You would need to fiddle around with the EXTRA constant to see what value is best.

Categories

Resources