Attatch elements to bottom of screen without using RelativeLayout

Go To StackoverFlow.com

0

Say I wanted to list buttons so that the last one was anchored to the bottom of the screen, how would I go about this. From my understanding, LinearLayout will give me the 'list' of buttons but it will be anchored to the top. RelativeLayout, on the other hand, will allow me to anchor to the bottom but each and every element will have to have a reference to the next element and the XML file will be in reverse order (so that references are valid).

How should I approach this?

EDIT: Apologies, this would be roughly what I would be looking for -

screenshot

2012-04-04 16:59
by stephenfin
To make sure we get this right, I would suggest you pop on your favourite graphics editor (paint, gimp...etc) and then draw what you want and add it as an image to your post. A relative layout sounds about right however I am sure others in the community along with myself would want to ensure we gave the right advice - Graham Smith 2012-04-04 17:04


1

i have a hard time understanding your question exactly. But i assume that what you want is a horizontal list of buttons which is anchored to the bottom of the screen?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- put your other views in here -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="cancel" />

        <Button
            android:id="@+id/button_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="5dp"
            android:text="ok" />
    </LinearLayout>
</LinearLayout>
2012-04-04 17:06
by Renard
android:layout_gravity="bottom" - of course - stephenfin 2012-04-04 17:14


2

You can try adding an extra LinearLayout with a weight of 1 to take up all of the remaining space, and push the list of buttons to the bottom of the Activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/widget32"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
<Button
    android:id="@+id/widget33"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />
</LinearLayout>

Sample XML is for a single button that gets pushed by a LinearLayout to the bottom of the Activity; root layout is Linear too.

2012-04-04 17:05
by NoName
Thanks! Worked well for me to position button at the bottom of the screen without RelativeLayoutKonayuki 2017-02-02 08:26
Ads