Leoa
Leoa

Reputation: 1187

Position ListView Below Buttons

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>

Upvotes: 0

Views: 2247

Answers (3)

Darwind
Darwind

Reputation: 7371

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.

Upvotes: 1

TouchBoarder
TouchBoarder

Reputation: 6492

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);

Upvotes: 1

Egor
Egor

Reputation: 40203

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.

Upvotes: 1

Related Questions