Android programming: button below scrollview or listview

How can you add buttons below a scrollview but located at the bottom of screen? Well, this is a quite common question.
If you simple add a button at the bottom of your layout file where there is a Scrollview or Listview above, you can experience bad results:

  • If the scrollview/listview is not full of items, the button position will be somewhere in the middle of the screen instead of bottom.
  • If the view is full of items, the button will disappear.
wrong_button_below_scrollview

Common problem: the buttons are not at the bottom of the screen

So, how can you solve it? The trick is to use RelativeLayout instead of LinearLayout and the android:layout_alignParentBottom=”true” keyword.

Let’s see an example code for scrollview:

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”vertical” >

<ScrollView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” >

<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical” >

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:gravity=”center”
android:text=”some text” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:gravity=”center”
android:text=”some text2″ />
</LinearLayout>
</ScrollView>

<LinearLayout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:orientation=”horizontal” >

<Button
style=”?android:attr/buttonStyleSmall”
android:layout_width=”wrap_content”
android:layout_height=”40dp”
android:layout_gravity=”bottom”
android:layout_marginRight=”3dp”
android:layout_marginTop=”1dp”
android:layout_weight=”1″
android:text=”button1″ />

<Button

style=”?android:attr/buttonStyleSmall”
android:layout_width=”wrap_content”
android:layout_height=”40dp”
android:layout_gravity=”bottom”
android:layout_marginRight=”3dp”
android:layout_marginTop=”1dp”
android:layout_weight=”1″
android:text=”button2″ />
</LinearLayout>

</RelativeLayout>

And the result:

button_below_scrollview

Using RelativeLayout the buttons can be below the scrollview at the bottom of the screen

Advertisements

One Response to Android programming: button below scrollview or listview

  1. def says:

    Great post thank you very much !
    Btw could you please escape this caracter ===> ”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: