Home > Software engineering >  How to get buttons to show over maps activity
How to get buttons to show over maps activity

Time:09-17

I have tried like 5 different ways to try and get buttons to show over a maps activity, but no matter what I try I can't seem to get them to show up, anybody know what I am doing wrong? This pertains to android studio

Here is code from activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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/currentloca"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:backgroundTint="#818181"
android:orientation="vertical"
android:visibility="visible">

<fragment
    android:id="@ id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="553dp"
    tools:context=".MapsActivity" />

<androidx.appcompat.widget.LinearLayoutCompat
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:orientation="horizontal">

<Button
        android:id="@ id/zoomin"
        android:layout_width="205dp"
        android:layout_height="wrap_content"
        android:onClick="onZoom"
        android:text="Zoom In" />

    <Button
        android:id="@ id/zoomout"
        android:layout_width="205dp"
        android:layout_height="wrap_content"
        android:onClick="onZoom"
        android:text="Zoom Out" />


</androidx.appcompat.widget.LinearLayoutCompat>

</FrameLayout>

CodePudding user response:

As suggested in comment, I think your need is a relative layout. Here is a example.

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


       <fragment
        android:id="@ id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="553dp"
        tools:context=".MapsActivity" />

         <LinearLayout
             android:layout_alignParentTop="true"
             android:layout_alignParentStart="true"
             android:layout_alignParentLeft="true"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content">

               <Button />
               <Button />

         </LinearLayout>
</RelativeLayout>

This way the buttons will appears in the top of the map fragment.

CodePudding user response:

You should use FrameLayout or its sort instead of LinearLayout. Below is just showing a structure for an explanation:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <Button />
        <Button />
    
    </LinearLayout>

</FrameLayout>

LinearLayout lays its child views on a horizontal or vertical line. While FrameLayout over-lays its children.

About your actual code, at first, replace (overwrite) the surrounding (outer most) LinearLayout:

<LinearLayout 
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/currentloca"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#818181"
android:orientation="vertical"
android:visibility="visible">

(...)

</LinearLayout>

with FrameLayout:

<FrameLayout 
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/currentloca"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#818181"
android:orientation="vertical"
android:visibility="visible">

(...)

</FrameLayout>

CodePudding user response:

Remove the first parent LinearLayout from your code. Updated code should look like this.

 <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:id="@ id/currentloca"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:backgroundTint="#818181"
    android:orientation="vertical"
    android:visibility="visible">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="555dp">

        <fragment
            android:id="@ id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="553dp"
            tools:context=".MapsActivity" />

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal">

            <Button
                android:id="@ id/zoomin"
                android:layout_width="205dp"
                android:layout_height="wrap_content"
                android:onClick="onZoom"
                android:text="Zoom In" />

            <Button
                android:id="@ id/zoomout"
                android:layout_width="205dp"
                android:layout_height="wrap_content"
                android:onClick="onZoom"
                android:text="Zoom Out" />


        </androidx.appcompat.widget.LinearLayoutCompat>
    </FrameLayout>

</RelativeLayout>

If you want your Views to potentially overlap, use a FrameLayout. If you want them displayed linearly, use a LinearLayout

CodePudding user response:

Well, you should use ConstriantLayout and LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:orientation="vertical"
    tools:context=".Activities.Maps">

    <fragment
        android:id="@ id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        map:layout_constraintEnd_toEndOf="parent"
        map:layout_constraintStart_toStartOf="parent"
        map:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@ id/zoomin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:onClick="onZoom"
            android:text="Zoom In" />

        <Button
            android:id="@ id/zoomout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_weight="1"
            android:onClick="onZoom"
            android:text="Zoom Out" />


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
  • Related