0

So I've been testing various ways to make the button appears as I want it to and been failing.

I'm trying to meet the following requirements that I've set for myself: A. The button must be at the bottom of the screen (known screen size if that helps in an answer) if the listview doesn't contain too many items B. The listview must push the button off-screen if it reaches it (this part I've solved by adding the button as a footer to the listview)

My problem is solving the first part. By adding it as a footer, it appears smack in the middle of the screen when the list contains few items. So I need help figuring a way to make it stay at the bottom till the listview gets big enough and then get pushed further down by the listview.

Here's my layout code so far:

<?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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <Button
          android:id="@+id/Remove"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Remove product(s)" />

</RelativeLayout>

Also, this is how i added the button so it's at the end of the listview:

LayoutInflater inflater = LayoutInflater.from(this);
  View v = inflater.inflate(R.layout.shopping_cart_activity, null);
  Button removeprod = (Button) v.findViewById(R.id.Remove);
  getListView().addFooterView(v);

I've looked up various answers here on stackoverflow, like this one. Unfortunately none of the answers there were concrete enough to solve it for me, maybe an example would of been more useful (new to this). I've also tried adding to the button the following:

android:layout_alignParentBottom="true"

and it didn't work. I've tried having the button in the layout for the listadapter as well and let's just say it turned out in x number of buttons, where x = how big the list was.

Any thoughts are greatly appreciated, thanks in advance. If you want me to add more to the post, let me know and I will edit as soon as possible.

Community
  • 1
  • 1

3 Answers3

3

One way I did it is to use a LinearLayout, and add a View with layout_weight="1", which takes up the rest of the screen so that the button is at the bottom. When the ListView gets big enough, the View will still have layout_weight="1", but it will have a height of 0dip. Below is the code that I used:

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


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>


    <View
        android:layout_width="0dip"
        android:layout_height="0dip"
        android:layout_weight="1"/>


    <Button
          android:id="@+id/Remove"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Remove product(s)" />

</LinearLayout>
  • The answer is great except for the fact that you can no longer see the button once the scroll kicks in. It will only scroll till the last item in the listview and stop. Otherwise it works perfectly. Any idea on how to solve the button issue? Or have you not encountered it? – user3588546 Jun 15 '14 at 02:51
  • @user3588546 hmm.. do you still have your code about `LayoutInflater inflater` in your `.java` file? If you use this method, I believe you don't need that code, and it might be what's causing the scrolling issue. If you want to try removing that segment of code to see if it works I can get a better idea of what's going on – user3728164 Jun 15 '14 at 03:03
  • I removed the code before trying it - sorry for the delay in answer. With the code the button appears right under the last product, without the code the button doesn't appear at all once the list triggers the scroll. – user3588546 Jun 15 '14 at 11:57
  • Almost solved my question - decided to pick it as best answer. I've solved my issues by moving the button to the top, making it a header instead of a footer. Not as elegant as I planned, but works. – user3588546 Jun 15 '14 at 21:55
0
<?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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <Button
          android:id="@+id/Remove"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerInParent="true"
          android:text="Remove product(s)" />

</RelativeLayout>

The trick here is to use android:layout_alignParentBottom = "true". Try this and let me know. And there's no need to specify orientation to RelativelLayout. It is the attribute of LinearLayout..

Aniruddha
  • 4,477
  • 2
  • 21
  • 39
  • I've tried that as stated in my post and it will display it at the end of the listview, but not at the bottom of the screen. :| – user3588546 Jun 15 '14 at 11:58
0

Use like below i am doing.your have set listview about footer layout,else yout last item in the list is not visible as you set height & width of listview to fill_parent and its better if you hardcore the height of your footer view.

<?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:layout_width="fill_parent"
                 android:layout_height="fill_parent" >


          <ListView
                android:id="@android:id/_list"
                android:above="@+id/footer"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>

          <Button 
                 android:id="@+id/footer"
                 android:layout_width="wrap_content"
                 android:layout_height="55dp"
                 android:layout_alignParentBottom="true"
                 android:layout_centerInParent="true"
                 android:text="Remove product(s)" />

 </RelativeLayout>
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59