Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 843
Muaz Usmani Avatar asked Jun 22 '12 16:06

Muaz Usmani


2 Answers

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>
like image 192
Akilan Avatar answered Oct 23 '22 19:10

Akilan


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>
like image 41
Aleks G Avatar answered Oct 23 '22 17:10

Aleks G