Jason Crosby
Jason Crosby

Reputation: 3573

Two Buttons on top two on bottom

I am looking to get 4 buttons on the screen. Two on top and tow on bottom. This code gives me the top buttons the way I want it. However when I change layout_alignParentTop="true" to layout_alignParentBottom="true" the buttons stay on top. They dont move to the bottom as expected. Any ideas?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout02" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_alignParentTop="true">
<Button 
    android:id="@+id/Button02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" android:text="test">
</Button>
<Button 
    android:id="@+id/Button03" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:text="test">
</Button>
</LinearLayout>

Upvotes: 1

Views: 3196

Answers (4)

Simon Dorociak
Simon Dorociak

Reputation: 33495

So you should create parent element as <RelativeLayout> and in its create two <LinearLayouts> where each contains two Button. and for align first layout to top you can use android:layout_alignParentTop="true" and for align second to bottom use android:layout_alignParentBottom="true"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#3B3B3B">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:gravity="center_horizontal"
        android:layout_alignParentTop="true">

        <Button 
            android:id="@+id/loginBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="Second"
            />

        <Button 
            android:id="@+id/loginBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="Second"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        >

        <Button 
            android:id="@+id/loginBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="Second"
            />

        <Button 
            android:id="@+id/loginBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="40dp"
            android:text="Second"
            />
    </LinearLayout>
</RelativeLayout>

Upvotes: 1

K_Anas
K_Anas

Reputation: 31466

Here your root layout is LinearLayout:

android:layout_alignParentTop is useless

add your LinearLayout to a RelativeLayout an you will get it work!!

or the best way is to use the atribute

android:layout_gravity

so to show buttons on Buttom of the screen you can use this layout:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >

        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="test" >
        </Button>

        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="test" >
        </Button>
    </LinearLayout>

Upvotes: 0

Simon
Simon

Reputation: 14472

Or you could use a relative layout with layoutBelow and layoutAbove. Relative layouts are great for this kind of layout.

Upvotes: 0

Marcio Covre
Marcio Covre

Reputation: 4556

Use android:layout_height="match_parent" on the linear layout

Upvotes: 0

Related Questions