The button under the list is 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 this does not work.

 <?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 displayed, I do not know why: /. any help would be helpful.

+7
source share
5 answers

List view takes up a full page. Try to give the desired weight to the code elements. 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> 
+9
source

This is because the ListView has a height set to wrap_content , which allows it to be expanded to fit all the items without leaving any screen space for the button. You can use the relative layout by setting the button at the bottom and then the list to take up 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> 
+8
source

I added this line .............. android: weight = "1" .......... as a list 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> 
+5
source

To do this, the button and the list should be in the same linearlayout, if all the views are in the relative layout, add listview and the button to the linear layout and give the weight of the list 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> 

+1
source

Adding weight or leveling the bottom will always make the button float at the bottom of the screen. And the list will be lower.

If you want to display the button after scrolling down the list, add the button as a footer to the list.

0
source