1

I'm trying to make a page with a listview that has some textviews and buttons beneath. I don't want it anchored at the bottom unless it would be pushed off otherwise. I was thinking of trying a relative layout with the listview aligned above a linearlayout but for some reason it doesn't show up at all

Code is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/feet">
    </ListView>

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@id/feet"
    >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/total"
            android:textSize="32sp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/funds"
            android:textSize="32sp" 
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/buy" 
            android:id="@+id/buyB"
            android:textSize="32sp" 
            android:onClick="confirmTrans"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/cancel" 
            android:id="@+id/canc"
            android:textSize="32sp" 
            android:onClick="cancelTrans"
            />
    </LinearLayout>
</RelativeLayout>
user2312638
  • 443
  • 2
  • 6
  • 17

2 Answers2

2

That's because by default a RelativeLayout positions its elements at the top-right. You will need to add a property to your LinearLayout such as

android:layout_alignParentBottom="true"

Right now, your LinearLayout will be positioned at the top-right and your ListView above that which there is no more room in the window for the ListView to go above the LinearLayout.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Wouldn't that anchor the elements to the bottom though? – user2312638 Aug 26 '13 at 20:19
  • Yes, which isn't what you want I see but that is why your `ListView` isn't showing. For what you are trying to do, you may need something like this if there are more elements in your `ListView` than what are showing. And if there isn't then you set this `View`s `visibility` to GONE and if there isn't then you use another `layout` that positions the `View` below your `ListView` – codeMagic Aug 26 '13 at 20:24
  • Which am I setting to gone? – user2312638 Aug 26 '13 at 20:27
  • It depends. If you have more items than can fit on the screen then you set the one directly below the `ListView` to `gone` and the other to `visible`. If all items fit on the screen then you set the one anchored to the bottom to `gone` and make the other `visible` – codeMagic Aug 26 '13 at 20:30
  • [This question/answer may help if you need to get the items that are visible](http://stackoverflow.com/questions/17953268/hide-custom-search-bar-if-all-items-in-adapter-are-showing) – codeMagic Aug 26 '13 at 20:39
  • Is there a way in code to figure out if I have more items than can fit on the screen? – user2312638 Aug 26 '13 at 20:44
  • Yes, see my previous comment – codeMagic Aug 26 '13 at 20:44
  • If I set the linear layout to gone won't that get rid of the buttons and texts? – user2312638 Aug 26 '13 at 20:47
  • Yes, but that's what you want if it doesn't go past the screen. Then you create another `LinearLayout` below the `ListView` and set that to `visible`. Or you could change the params of the `LinearLayout` in Java. But I think this would be easier and the impact on drawing the `UI` would be minimal if anything most likely – codeMagic Aug 26 '13 at 20:55
  • I didn't know you could create linearlayouts in the java. Is it possible to put the layout as it's own seperate view then just that beneath the linear layout (somehow)? – user2312638 Aug 26 '13 at 21:03
  • oh wait are you saying I make two linear layouts, one aligned to the bottom, one beneath the listview and default the one aligned to the bottom as visible and the one benath the listview as gone, and switch them if the list doesn't take up the whole screen? – user2312638 Aug 26 '13 at 21:32
  • You can create any `View` in Java but its usually easier in xml. Yes to the last comment – codeMagic Aug 27 '13 at 01:00
0

If you have such problem in ListView make sure that you use proper inflation method. Parent view group must be specified for correct inflation.

mLayoutInflater.inflate(R.layout.listitem, parent, false);

Instead of

mLayoutInflater.inflate(R.layout.listitem, null);
Oleksii Masnyi
  • 2,922
  • 2
  • 22
  • 27