0

I would like to add a Button on top of my default Googlemap Activity in android studio. I tried this one by changing the XML code of Googlemap fragment like this:

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

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

<fragment
    android:id="@+id/location_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />
<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:padding="0dp"
    android:paddingBottom="0dp"
    android:background="#359c5e"
    android:text="@string/go"
    android:textColor="#ffffff" />

But I am unable to get Button on my mapfragment. I tried so many ways by writing Button before Fragment and Viceversa in XML file, still facing the same problem.

My requirement is that

  1. to create a Button on mapfragment .
  2. when it is clicked it should show some customized markers on it.

How to achieve this task ? Any suggestions are appreciated.

Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
Abhinay
  • 27
  • 1
  • 1
  • 7
  • please explain more about the second requirement please . – Sagar Nayak Mar 29 '16 at 07:31
  • you want the button on the map fragment or you want the button above the map fragment. please be specific . – Sagar Nayak Mar 29 '16 at 07:35
  • Is button on the map fragment and button above the map fragment are different ? – Abhinay Mar 29 '16 at 09:24
  • you want the map all over the screen and then button covering some part of map . ? or you want the button on screen but does not hide any part of the map (here the button will not be over the map , it will be at one side and map will not cover whole screen ) ? – Sagar Nayak Mar 29 '16 at 09:29
  • Yes, got it. I want the map all over the screen and then button placed at top right corner of the map fragment which should show customized markers on clicking it. – Abhinay Mar 29 '16 at 10:02
  • do you have those markers ready to be placed on button click or you have to do some work on it ? – Sagar Nayak Mar 29 '16 at 10:17
  • yes, I have 5 markers with lat & lng coordinates, when i click the button on mapfragment then all the 5 markers should display on the map. – Abhinay Mar 29 '16 at 10:28
  • so where are you having the problem ? in layout ? – Sagar Nayak Mar 29 '16 at 10:30
  • provide your complete layout . this layout does not have some tags . – Sagar Nayak Mar 29 '16 at 10:31
  • Above code which is specified in the question is my complete layout. But unable to get Button on map fragment. – Abhinay Mar 29 '16 at 10:40

1 Answers1

6

use this layout .this can solve your problem.(for first requirement)

this will make the map fragment occupy all the screen first and then the button will be on the top right corner of the screen. as the map is all over the screen the button will be over the map and some part of map will be covered.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<fragment
    android:id="@+id/location_map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#359c5e"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:padding="8dp"
    android:layout_margin="5dp"
    android:text="@string/go"
    android:textColor="#ffffff" />

</RelativeLayout>

for second requirement

you can use a onclicklistener for the button and display those markers on the map.

let me know how it worked for you.

Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52