ddk
ddk

Reputation: 101

Aligning Button to Bottom of Screen

I have looked at a lot of answers and not found one that isn't a work-around. So, excuse me if this has been asked and answered.

I have a RelativeLayout that I want to appear at the bottom of the screen regardless of the size of the stuff outside the RelativeLayout and above it. Can this work as designed (Relative below Relative)? If so, please tell me what I'm doing wrong.

Note: I have removed most of the guts that make it obvious why the first RelativeLayout has to be a RelativeLayout and the 2nd RelativeLayout is usually an <include... that I use in other places and put in explicitly for this example.

Thanks.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/enter_Game_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:hint="Enter Game Name"
        android:inputType="text" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/d_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@android:string/cancel" />
</RelativeLayout>

Here's what I get...

enter image description here

Upvotes: 0

Views: 153

Answers (2)

Sharjeel
Sharjeel

Reputation: 15798

You parent layout is a linear layout. Make it a Relative layout. Then Put two Views (Edit Text and Button) in it and set one to align top and one to align bottom.

Your code would look like something like this (not tested)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


<EditText
        android:id="@+id/enter_Game_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:hint="Enter Game Name"
android:layout_alignParentTop="true"

        android:inputType="text" />

<Button
        android:id="@+id/d_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@android:string/cancel" />
</RelativeLayout>

Upvotes: 0

Omar HossamEldin
Omar HossamEldin

Reputation: 3111

In your second RelativeLayout you have android:layout_alignParentBottom="true" , this attribute will not work because the parent is a LinearLayout where this attribute doesn't work and has no effect, make you parent layout RelativeLayout instead of LinearLayout and remove the Orientation attribute since it will has no effect, then you will get the button at the bottom of the screen no matter the size of the screen and layout above your Button

Upvotes: 1

Related Questions