1

In a layout file I have a Listview whose size can grow/shrink dynamically. I have a button btn_rec_add and it's click event I add an item in the ListView. I have tried many changes in the Layout file but haven't been able to make the button shift its location based on number of items in the ListView. If I keep the button in the same RelativeLayout which has the ListView, then the button moves dynamically which is exactly how I want but I can't see the button after adding 5 or more elements in 4.3 inch display phones. If I keep the button outside the RelativeLayout of the ListView, then it is fixed on the screen.

Currently, the btn_rec_add is fixed to the bottom of the layout. Can someone please help me solve this problem.

Here is the XML code:

 <?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"
android:background="@drawable/bg" >

<ImageView
    android:id="@id/top_bar_view"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/top_bar"
    android:contentDescription="@string/content" />

<TextView
    android:id="@+id/txt_recipients"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="8dp"
    android:padding="8dp"
    android:text="@string/text_recipients"
    android:textColor="#FFFFFF"
    android:textSize="16sp" />

<ImageButton
    android:id="@id/btn_back"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/content"
    android:paddingTop="6dp"
    android:src="@drawable/ic_back" />

<RelativeLayout
    android:id="@+id/Rlayout_recipients"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/btn_rec_add"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/top_bar_view" >

    <ListView
        android:id="@+id/rec_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:paddingTop="20dp" />
</RelativeLayout>

<ImageButton
    android:id="@+id/btn_rec_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/content"
    android:src="@drawable/icon_add" />

</RelativeLayout>

How it looks currently

Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
user2688158
  • 407
  • 5
  • 17

2 Answers2

0

If I understand correctly, you want the behavior of the button to be as follows:

  • Appear below the last ListView item if the ListView does not extend to fill screen
  • If the ListView extends the full height of the screen, the button should be at the bottom of the screen, but the list should remain scrollable

If my understanding is correct, you should place your ListView and your button in a LinearLayout as follows:

<LinearLayout
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button 
        android:layout_width="match_parent" 
        android:layout_height="@dimen/button_height"
        android:background="@drawable/button_image" />

</LinearLayout>

The effect of the above layout is as follows:

  • Layout in which items are vertically placed
  • Layout which will be as wide as parent, but as tall as ListView and Button
  • ListView will take up all of the space in the layout that the button does not occupy (this is layout_weight="1" whereas Button has no layout weight so it will simply fill as much space as it needs as defined in this case by @dimen/button_height)

Great Android layout question!

Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
  • Make sure you understand what is going on with the `LinearLayout`. They are really useful layouts! If you learned from this in addition to solving your issue, go ahead and upvote it as well: http://meta.stackexchange.com/a/198551/187616 – Daniel Smith Feb 12 '14 at 08:28
-1

Your Problem is related to User Experience. You have to decide whether user will like to scroll to end of the list to press add button or user want to add without scrolling to end of list. Since you only have two options with our scenario, either keep add button fixed or add it as footer of listview.

Pankaj
  • 1,242
  • 1
  • 9
  • 21