Muaz Usmani
Muaz Usmani

Reputation: 1318

Button under ListView not visible in Android

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?

Upvotes: 10

Views: 10811

Answers (5)

Akilan
Akilan

Reputation: 1717

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>

Upvotes: 16

Murli
Murli

Reputation: 542

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>

Upvotes: 1

Abu Ruqaiyah
Abu Ruqaiyah

Reputation: 1546

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.

Upvotes: 0

Cloy
Cloy

Reputation: 2181

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>

Upvotes: 6

Aleks G
Aleks G

Reputation: 57306

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>

Upvotes: 9

Related Questions