Button under ListView not visible in Android
Asked Answered
B

5

10

I am working on some ListView I want to display a Button under it I am using the following code but that is not working.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:drawSelectorOnTop="false" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>

the button is not visible, why?

Berard answered 22/6, 2012 at 16:38 Comment(2)
Try giving it a defined width value instead of wrap_content, i.e. '150dp'.Glutamine
I tried this and the listview bled into the button area.Eyeleteer
L
16

List view takes the full page. Try to give desired weight to the elements to your code. Use this code,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:drawSelectorOnTop="false"
    android:layout_weight="5" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_weight="1" />

</LinearLayout>
Lecher answered 22/6, 2012 at 16:51 Comment(1)
Nicer to use android:layout_weight instead of android:weight.Neisse
E
9

This is happening because ListView has height set to wrap_content, which makes it extend to accommodate all items leaving no space on the screen for the button. You can use relative layout setting the button along the bottom and then the listview to occupy the remaining space:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_alignParentBottom="true" />

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#00000000"
    android:drawSelectorOnTop="false"
    android:layout_above="@id/button1" />

</RelativeLayout>
Egyptian answered 22/6, 2012 at 16:42 Comment(2)
This will make the button float at the bottom of the screen always. Not the effect where you can scroll the list and find the button.Ferric
But that's not what the op was asking about.Egyptian
P
6

I have added this line.............. android:weight="1" .......... within the list view as below

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:drawSelectorOnTop="false" 
    android:weight="1" 
/>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>
Prosy answered 6/7, 2015 at 11:31 Comment(2)
You should write down your changes, we have to find diferrences in yours and OP codeKlecka
I have added this line.............. android:weight="1" ..........within the list viewProsy
R
1

For this the button and the listview should be in a same linearlayout, if all views are in relative layout, add the listview and button to a linear layout and give listview weight as 1, this worked for me.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_shipping_address"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_all"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.reallymake.android.pottery.ShippingAddressActivity">

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView9"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="13dp"
    android:background="@drawable/button_shape"
    android:text="@string/ok"
      />

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/btn_add_new_address"
    android:orientation="vertical">


    <ListView
        android:id="@+id/lv_addresses"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/list"
        android:layout_marginTop="14dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lv_addresses"
        android:layout_marginTop="14dp"
        android:background="@drawable/button_shape"
        android:text="@string/proceed" />
</LinearLayout>

Rhyme answered 20/2, 2017 at 6:1 Comment(0)
F
0

Adding weight, or aligning bottom will make the button float at the bottom of the screen always. And the list will go below it.

If want to display the button after scroll down the list, then add the button as a footer to the list view.

Ferric answered 15/6, 2016 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.