0

I have a ListView that is appearing behind two buttons. I'd like the ListView to fill the area below the buttons. I've tried the solutions in this link: Android Layout with ListView and Buttons and Listview with button below not working properly . However, it is not working. Can you tell me how to keep theListView below the buttons?

enter image description here

Here is my code:

<?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:layout_weight="1.0"
    >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
           android:layout_below="@id/layoutButtons"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:weightSum=".50"  />

        <LinearLayout
 android:id="@+id/layoutButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
           android:layout_weight="1.0" >

        <Button
            android:id="@+id/btnLogout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".70"
            android:text="left" />

        <Button
            android:id="@+id/btnMessage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".30"
            android:text="right" />
    </LinearLayout>

</RelativeLayout>
Community
  • 1
  • 1
Leoa
  • 1,167
  • 2
  • 14
  • 31

3 Answers3

1

Try the following:

<?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"
    >

        <LinearLayout
 android:id="@+id/layoutButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
           android:orientation="horizontal" >

        <Button
            android:id="@+id/btnLogout"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".70"
            android:text="left" />

        <Button
            android:id="@+id/btnMessage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".30"
            android:text="right" />
    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
1

You can add the buttons in the header of the listview:

Create a layout with the buttons listview_header.xml

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutButtons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
       android:layout_weight="1.0" >

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".70"
        android:text="left" />

    <Button
        android:id="@+id/btnMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".30"
        android:text="right" />
</LinearLayout>     

Declare views:

 private ListView listView;
 private ViewGroup mListHeaderView;

onCreateView()

 mListHeaderView = (ViewGroup)inflater.inflate(R.layout.listview_header,
 listView, false);

onActivityCreated()

listView = this.getListView();
listViewTickets.addHeaderView(mListHeaderView,null,false);
TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
  • 1
    The buttons won't be fixed on the top of the screen when adding them to the header of the ListView. When scrolling in the ListView, they will disappear out of the screen eventually. – Darwind Sep 28 '12 at 17:38
  • Thanks, this is a better implementation. – Leoa Sep 28 '12 at 23:58
1

Modified Leoa's answer a bit:

<LinearLayout
    android:id="@+id/layoutButtons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.0" >

    <Button
        android:id="@+id/btnLogout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".70"
        android:text="left" />

    <Button
        android:id="@+id/btnMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".30"
        android:text="right" />
</LinearLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/layoutButtons"
    android:weightSum=".50" />

Removed the

android:layout_alignParentTop="true"

from the ListView and changed the id of the ListView to @android:id/list. Also the ListView must be placed below the LinearLayout with the buttons, else you won't be able to use android:layout_below in the ListView.

Darwind
  • 7,284
  • 3
  • 49
  • 48