Shruiken Shruiken - 3 years ago 208
Android Question

Adding 2 Buttons relatively to one another on google maps api for android

I want to add 2 Buttons to my Android map app. They should be on the top left edge in front of my map just like the "find your position" button of google maps itself.

In my activity_maps.xml i have:

<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|top"
android:text="@string/start_button_text"
android:padding="10dp"
android:layout_marginTop="9dp"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:onClick="start_button_clicked"
/>

<Button
android:id="@+id/vorschlag_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/start_button"
android:text="@string/vorschlag_button_text"
android:padding="10dp"
android:layout_marginTop="9dp"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:onClick="vorschlag_button_clicked"
/>


My problem is the second button (@+id/vorschlag_button) is displayed on top of the first button (@+id/start_button) instead of right of the start button.

Also is there a way to align my buttons to the "my_position_button" of Google Maps API?

I also would like to have the same background color like the "my_position_button".

I get the error message on

android:layout_toRightOf="@+id/start_button"


"Invalid layout param in a fragment."

Thank you for your help with this.

P.S.: Since it's not that much more: My whole activity_maps.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:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">

<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|top"
android:text="@string/start_button_text"
android:padding="10dp"
android:layout_marginTop="9dp"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:onClick="start_button_clicked"
/>
<Button
android:id="@+id/vorschlag_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/start_button"
android:text="@string/vorschlag_button_text"
android:padding="10dp"
android:layout_marginTop="9dp"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:onClick="vorschlag_button_clicked"
/>

</fragment>

Answer Source

Use Relative layout it will solve your issue. In this code if you want to button to bottom than add android:layout_alignParentBottom="true" to both button.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/start_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start|top"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="9dp"
        android:onClick="start_button_clicked"
        android:padding="10dp"
        android:paddingRight="10dp"
        android:text="start_button_text" />

    <Button
        android:id="@+id/vorschlag_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="9dp"
        android:layout_toRightOf="@+id/start_button"
        android:onClick="vorschlag_button_clicked"
        android:padding="10dp"
        android:paddingRight="10dp"
        android:text="vorschlag_button_text" />

</RelativeLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download