1

I am trying to include a ListView within an Activity that is running inside a Dialog. The dialog is basically used to search for bluetooth devices, so I need two Button's, a TextView and a ListView. Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:padding="20dp">
    <TextView
        android:text="Bluetooth List"
        android:paddingTop="15dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
    </TextView>
    <ListView android:id="@+id/bluetoothPanel_lvBluetooth"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_margin="10dip"
        android:drawSelectorOnTop="false">
    </ListView>
    <LinearLayout
        android:orientation="horizontal"
        android:paddingLeft="4.0dip"
        android:paddingTop="5.0dip"
        android:paddingRight="4.0dip"
        android:paddingBottom="1.0dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
            <Button
                android:id="@+id/bluetoothPanel_btnSearch"
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:text="Search"
                android:layout_weight="1.0" />
            <Button
                android:id="@+id/bluetoothPanel_btnCancel"
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:text="Cancel"
                android:layout_weight="1.0" />
    </LinearLayout>
</LinearLayout>

At the beginning, the ArrayAdapter that is within the ListView is empty, and I am dynamically add elements to it.

The problem is, I want to set a fixed size for the ListView so that it won't change its size upon adding elements dynamically (List of bluetooth devices are shown). Could you please help me to fix my XML?

EDIT:

I am not talking about array size. I am talking about the listview itself (the view), the height of the listview.

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417

2 Answers2

0

Set a custom ListAdapter for the list and override the getCount() method so it returns always your fixed size.

Example: http://jnastase.alner.net/archive/2010/12/19/custom-android-listadapter.aspx

Stephan
  • 4,395
  • 3
  • 26
  • 49
  • Oh, I am not talking about array size. I am talking about the listview itself (the view) – Eng.Fouad Oct 31 '11 at 23:26
  • Usually the view size depends on the array size. If the array size is fixed, the view will stay at the same size. – Stephan Oct 31 '11 at 23:30
  • but the array I am using is not fixed, and it depends on how many bluetooth devices are around :) – Eng.Fouad Oct 31 '11 at 23:32
  • 1
    Ah ok, misunderstood. You can set a fixed height for the list, if that is what you want to do: e.g. `layout_width="50dip"` – Stephan Oct 31 '11 at 23:35
  • I tried this before but it does not change the size, maybe because I run the activity on a dialog. – Eng.Fouad Oct 31 '11 at 23:37
  • Maybe you need to wrap your layout in a scroll view: http://stackoverflow.com/questions/4055537/android-linearlayout-scroll – Stephan Oct 31 '11 at 23:40
  • I've tried this too, and this doesn't change the height but the scrollbar doesn't move – Eng.Fouad Oct 31 '11 at 23:45
  • Did you set `android:layout_height="wrap_content"` for the LinearLayout when you wrapped it in a ScrollView? – Stephan Oct 31 '11 at 23:54
  • Yes, and it displays only one element of the list – Eng.Fouad Oct 31 '11 at 23:57
  • Weird :( You could try to use RelativeLayout, align the list below the TextView and above the buttons layout. I used this to have headers and footers on layouts. But no idea if it works on dialogs. – Stephan Nov 01 '11 at 00:07
0

I think you are asking how to reserve a fixed-size space for a dynamically changing ListView. In a similar situation, I wrapped the ListView in a LinearLayout, as shown below. Note that this is the xml for the ListView itself, not for the cell contents. The fixed height in this example is 100dip x 200dip.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background_image"
    android:layout_width="100dip"
    android:layout_height="200dip">

    <ListView   android:id="@+id/list_view"
                android:layout_width="100dip"
                android:layout_height="match_parent"
                android:cacheColorHint="#00000000"
    />
</LinearLayout>

Notice that the layout_width of the ListView also is set to the desired fixed width. I'm not sure why, but this seems to be necessary.

Also notice that the background image or color is set on the LinearLayout, not on the ListView. Setting the ListView's cacheColorHint to transparent makes the ListView play nicely.

If you were asking something else (perhaps, for example, fixing the scrollable height regardless of elements, which seems to be answered by @Stephen), you may want to clarify the question.

SansWit
  • 310
  • 3
  • 8