user3588546
user3588546

Reputation: 75

How to have a button start at bottom of screen, but also end of listview?

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.

Upvotes: 0

Views: 944

Answers (3)

Pankaj Arora
Pankaj Arora

Reputation: 10274

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>

Upvotes: 0

Aniruddha
Aniruddha

Reputation: 4487

<?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..

Upvotes: 0

user3728164
user3728164

Reputation: 31

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>

Upvotes: 3

Related Questions