jesses.co.tt
jesses.co.tt

Reputation: 2729

Android - can't get relative layout to align to parent bottom

I am trying to add a custom button on top of a (GoogleMap though I'm not sure how relevant that is...) fragment.

I don't want it to sit underneath it per se - like I could do with a FrameLayout or LinearLayout - but I want to the button to sit on top of the map with its parent layout being transparent...

This is what I (currently) have in my xml:

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"
    map:mapType="normal"
    tools:layout="@layout/activity_location">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" // THIS DOESNT WORK !!!
        android:background="#FC0" // for debug purposes
        android:gravity="center|bottom" >

        <Button
            android:id="@+id/location_order_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/small_margin"
            android:background="@drawable/nav_btn_send"
            android:text=""
            android:textColor="@color/black"
            android:textSize="@dimen/loginFontSize" />
    </RelativeLayout>

</fragment>

Note the comments on the 'android:layout_alignParentBottom' and 'android:background' lines...

This gives me this result :

enter image description here

Now... you can tell through the code that I am attempting to align this to the screen/map bottom... but whatever I do, it is aligning to the top, underneath the ActionBar...

I have tried a few other approaches that I found on SO, but this is the closest result to what I want so far...

Any idea what I am doing wrong ?

Upvotes: 0

Views: 2893

Answers (3)

Ethan
Ethan

Reputation: 219

Align parent bottom is an invalid layout param for fragments. Try this: Set the height of the relative layout to match_parent and align the button at the bottom of the relative layout.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FC0">

    <Button
        android:id="@+id/location_order_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/small_margin"
        android:layout_alignParentBottom="true"
        android:background="@drawable/nav_btn_send"
        android:text=""
        android:textColor="@color/black"
        android:textSize="@dimen/loginFontSize" />
</RelativeLayout>

Upvotes: 0

Mimmo Grottoli
Mimmo Grottoli

Reputation: 5773

Try this:

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

<fragment
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    class="com.google.android.gms.maps.SupportMapFragment"
    map:mapType="normal"
    tools:layout="@layout/activity_location"/>

    <Button
        android:id="@+id/location_order_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/small_margin"
        android:background="@drawable/nav_btn_send"
        android:text=""
        android:textColor="@color/black"
        android:textSize="@dimen/loginFontSize"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"/>

</RelativeLayout>

Upvotes: 1

Buddy
Buddy

Reputation: 2114

You should define your fragment inside RelativeLayout in this way:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="#FC0"
    android:gravity="center|bottom" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
        map:mapType="normal"
        tools:layout="@layout/activity_location" />

    <Button
        android:id="@+id/location_order_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/small_margin"
        android:background="@drawable/nav_btn_send"
        android:text=""
        android:layout_alignParentBottom="true"
        android:textColor="@color/black"
        android:textSize="@dimen/loginFontSize" />
</RelativeLayout>

and you should use layout_alignParentBottom property in the child element that you want to align at the bottom of the parent, in this case, it is Button

Upvotes: 2

Related Questions