views:

3459

answers:

7

Alright, this specific layout is just annoying me. And can't seem to find a way to have a listView, with a row of buttons at the bottom so that the listview doesn't extend over top of the buttons, and so the buttons are always snapped to the bottom of the screen. Here's what I want: alt text

It seems like it should be so easy, but everything I've tried has failed. Any help?

Here's my current code:

    RelativeLayout container = new RelativeLayout(this);
    container.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    //** Add LinearLayout with button(s)

    LinearLayout buttons = new LinearLayout(this);

    RelativeLayout.LayoutParams bottomNavParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    bottomNavParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    bottomNavParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttons.setLayoutParams(bottomNavParams);


    ImageButton newLayer = new ImageButton(this);
    newLayer.setImageResource(R.drawable.newlayer);
    newLayer.setLayoutParams(new LinearLayout.LayoutParams(45, LayoutParams.FILL_PARENT));
    buttons.addView(newLayer);

    container.addView(buttons);

    //** Add ListView

    layerview = new ListView(this);

    RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    listParams.addRule(RelativeLayout.ABOVE, buttons.getId());

    layerview.setLayoutParams(listParams);

    container.addView(layerview);
+4  A: 

The best way is a relative layout that sets the buttons below the listview. In this example the buttons are also in a linear layout because it is easier to put them side by side at an equal size.

<RelativeLayout android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<ListView android:id="@+id/ListView01" 
android:layout_alignParentTop="true"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">
</ListView>

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_below="@+id/ListView01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_alignParentBottom="true">
<Button android:id="@+id/ButtonJoin" 
android:text="Join"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true">
</Button>
<Button android:id="@+id/ButtonJoin" 
android:layout_alignRight="@id/ButtonCancel" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:layout_alignParentBottom="true">
</Button>
</LinearLayout>

</RelativeLayout>
sadboy
The problem is that it extends over top of the Buttons at the bottom if the listview is too long, though. I can't figure out how to get it to stop above the buttons.
GuyNoir
If you are using (I think 1.5, maybe even 1.6) you MUST have the buttons BEFORE the ListView when using RelativeLayout rules - otherwise, the layout is not aware of the Buttons yet because it reads them in order, which is why your ListView extends past them.
Tim H
This does not work for me. Like Tim H says, for me I must put the ListView after the LinearLayout and then make the ListView layout_above, like repoc's answer above.
Nemi
+7  A: 

I think this is what you are looking for.

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

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/testbutton"
        android:text="@string/hello" android:layout_alignParentBottom="true" />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/list"
        android:layout_alignParentTop="true" android:layout_above="@id/testbutton" />

</RelativeLayout>
repoc
That's what I've basically been using, though I'm writing the layout in Java. The listView still extends over the buttons.
GuyNoir
The only thing I'm changing is that I'm adding a relativeLayout around the ListView because I can't call addRule(RelativeLayout.ABOVE) on a ListView.
GuyNoir
This code should work fine. `android:layout_above="@id/testbutton"` will keep the `ListView` above the `Button`.
CommonsWare
When i use this I get the list to stop above the button and then start scrolling internally.
repoc
You don't call `addRule(RelativeLayout.ABOVE)` on a `RelativeLayout`, either. You call it on a `RelativeLayout.LayoutParams`. You then add the `ListView` to the `RelativeLayout`, supplying that `RelativeLayout.LayoutParams`. Please consider switching to XML and inflating it, for long-term maintainability.
CommonsWare
I edited in my code in my first post. If all else fails I'll try out the XML route. My only problem with XML is it's hard to make things on the fly, which my application does a lot of.
GuyNoir
But you can always create your layouts in xml and get all components in java and set visible on the components that you don't want to have in current layout.
repoc
Alright, the XML worked after inflating it. Point taken. :P God knows why it didn't work in Java. Thanks.
GuyNoir
Heh, I know I'm late, just ran into this problem myself, and this XML helped me as well! Thanks!
kcoppock
+1  A: 

I know this post is rather old, but, to answer the original poster's question, the reason the code did not work was buttons.getId() returns -1. If you are going to do this, you need to set do something like call buttons.setId(10). If you do that, the code works just fine.

Scott Vickery
A: 

I had the same problem for ages.

The solution to keeping the ListView above the buttons, but preventing it from covering them up when the list is long, is to set android:layout_weight="1.0" on the ListView. Leave the layout_weight on the buttons unset so that they remain at their natural size, otherwise the buttons will get scaled. This works with LinearLayout.

There's an example in the Android ApiDemos: ApiDemos/res/layout/linear_layout_9.xml

David Ingram
A: 

I've tried the solutions out but when the ListView starts scolling I loose the button below :( There must be some other solution?

PADE
A: 

thanks a lot by two coders . we spend two days for this so, thank you very much

rakesh