Search code examples
androidgoogle-maps-api-2infowindow

How to add objects in an InfoWindow making it larger



I need to have a Button and a ProgressBar in the InfoWindow of my markers. I have two main problems that are:

  1. The InfoWindow is not large enaugh to contain the text I need.
  2. I don't know how to add any kind of object inside InfoWindow.

Here is how it looks like in my map and the code I use to generate a marker:

My Map

MarkerOptions markerOptions = new MarkerOptions().position(enemy.position).title("Mostro " + enemy.nome).snippet("Livello " + enemy.level + "\n" + enemy.health());
map.addMarker(markerOptions);

As you can see the rest of the text I wrote in the snippet isn't shown.

How can I solve these two problems?
Thank you in advance for your patience and consideration.


Solution

  • Create a custom layout for info window as shown below and initialize your infowindowadapter with this layout:

    ********InfoWindow********

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    
        <RelativeLayout
                android:layout_gravity="center"
                android:layout_width="300dp"
                android:layout_height="wrap_content">
    
            <TextView
                    android:id="@+id/location_tv"
                    android:textStyle="bold"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:gravity="center"
                    android:text="BluePearl Veterinary Partners"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                    android:textColor="@color/about_color"
                    android:layout_centerHorizontal="true"/>
    
            <LinearLayout
                    android:layout_marginTop="16dp"
                    android:gravity="center_vertical"
                    android:layout_below="@+id/location_tv"
                    android:id="@+id/direction_layout"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
    
    
                <ImageView
                        android:id="@+id/direction_imageview"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:scaleType="fitXY"
                        android:layout_marginStart="16dp"
                        android:src="@drawable/ic_place" />
    
                <TextView
                        android:id="@+id/address_tv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginEnd="16dp"
                        android:text="1 West 15th Street, New York, NY, 10011"
                        android:layout_toRightOf="@+id/direction_imageview"
                        android:layout_below="@+id/location_tv"
                        android:textSize="13sp"
                />
    
    
            </LinearLayout>
    
            <LinearLayout
                    android:layout_marginTop="12dp"
                    android:gravity="center_vertical"
                    android:id="@+id/phone_layout"
                    android:layout_below="@+id/direction_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                <ImageView
                        android:id="@+id/phone_imageview"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginStart="16dp"
                        android:layout_alignParentLeft="true"
                        android:layout_below="@+id/direction_imageview"
                        android:src="@drawable/phone_red" />
    
                <TextView
                        android:id="@+id/ph_tv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:textSize="13sp"
                        android:text="212-924-3311"
                        android:layout_toRightOf="@+id/phone_imageview"
                        android:layout_below="@+id/address_tv" />
    
            </LinearLayout>
    
    
    
    
            <View
                    android:layout_below="@+id/phone_layout"
                    android:layout_width="match_parent"
                    android:layout_height="16dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"/>
    
    
    
        </RelativeLayout>
    
    </LinearLayout>
    

    ********InfoWindow.kt********

    class InfoWindow(private val context: Context) : GoogleMap.InfoWindowAdapter {
    
        override fun getInfoWindow(marker: Marker): View? {
            return null
        }
        override fun getInfoContents(marker: Marker): View {
            val view = (context as Activity).layoutInflater
                .inflate(R.layout.info_window_map, null)
            val location_tv = view.findViewById<TextView>(R.id.location_tv)
            val address_tv = view.findViewById<TextView>(R.id.address_tv)
            val phone_tv = view.findViewById<TextView>(R.id.ph_tv)
            val directoryModel = marker.tag as HospitalsList?
            if (directoryModel != null) {
                location_tv.text = directoryModel.title
                address_tv.text = directoryModel.address
                phone_tv.text = directoryModel.contactnumber
            }
            return view
        }
    }
    

    ********MapActivity.kt********

    in your MapActivity where you are initilizing your map fragment use these line of code:

    val customInfoWindow = InfoWindow(this)
       map.setInfoWindowAdapter(customInfoWindow)