Home » Android » android – Click on marker -> getInfoContents not show my custom widget-Exceptionshub

android – Click on marker -> getInfoContents not show my custom widget-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

Android studio:

implementation ‘com.google.android.gms:play-services-maps:17.0.0’

in my activity:

import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback

private lateinit var mMap: GoogleMap

 mMap.setInfoWindowAdapter(object : GoogleMap.InfoWindowAdapter {
            override fun getInfoWindow(marker: Marker): View? {
                return null
            }

            // Important! Not work with "androidx.constraintlayout.widget.ConstraintLayout"
            override fun getInfoContents(marker: Marker): View? {
                val markerLatLng = marker.position
                val selectGazStation =
                    gazStationsList.first { it.latitude == markerLatLng.latitude && it.longitude == markerLatLng.longitude }
                Debug.d(TAG, "getInfoContents: selectGazStation = $selectGazStation")
                val agentSummaryInfoWidget = AgentSummaryInfoWidget(application, selectGazStation)
                return agentSummaryInfoWidget
            }
        })
    }

Here my custom widget AgentSummaryInfoWidget

class AgentSummaryInfoWidget(context: Context, selectGazStation: GazStation) :
    LinearLayout(context) {

    init {
        init(context, selectGazStation)
    }

    companion object {

        private val TAG = AgentSummaryInfoWidget::class.java.name
    }

    private fun init(context: Context, selectGazStation: GazStation) {
        val mapMarkerInfoContentLayoutBinding =
            MapMarkerInfoContentLayoutBinding.inflate(LayoutInflater.from(context));
        mapMarkerInfoContentLayoutBinding.addressValueTextView.setText(selectGazStation.address)
        var workingHours = "24/24"
        if (!selectGazStation.workingHours?.contains("NON-STOP", true)!!) {
            workingHours = selectGazStation.workingHours!!
        }
        mapMarkerInfoContentLayoutBinding.workingHoursValueTextView.setText(workingHours)
        mapMarkerInfoContentLayoutBinding.phoneValueTextView.setText(selectGazStation.phone)
    }
}

Here xml map_marker_info_content_layout:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/titleContainerLayout"
            android:layout_width="match_parent"
            android:layout_height="@dimen/min_height"
            android:layout_marginStart="@dimen/half_default_margin"
            android:layout_marginTop="@dimen/default_margin"
            android:layout_marginEnd="@dimen/half_default_margin"
            android:background="@drawable/bottom_border_bg"
            android:orientation="vertical">

            <TextView
                android:id="@+id/titleTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:text="Rompetrol"
                android:textColor="@android:color/black"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/stateTtextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:text="@string/open"
                android:textColor="@android:color/holo_green_light"
                android:textSize="14sp"
                app:layout_constraintBottom_toBottomOf="parent" />
        </LinearLayout>


        <LinearLayout
            android:id="@+id/agentInfoContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bottom_border_bg"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/containerAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginTop="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin">

                <TextView
                    android:id="@+id/addressLabelTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/half_default_margin"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:text="@string/address_colon" />

                <TextView
                    android:id="@+id/addressValueTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:gravity="end" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/containerWorkingHours"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginTop="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin">

                <TextView
                    android:id="@+id/workingHoursLabelTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/half_default_margin"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:text="@string/working_hours_colon" />

                <TextView
                    android:id="@+id/workingHoursValueTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:gravity="end"
                    app:layout_constraintEnd_toEndOf="parent" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/containerPhon"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginTop="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin"
                android:layout_marginBottom="@dimen/half_default_margin">

                <TextView
                    android:id="@+id/phoneLabelTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/half_default_margin"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:text="@string/phone_colon" />

                <TextView
                    android:id="@+id/phoneValueTextView"
                    style="@style/mapMarkerInfoItemLabelTextStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="@dimen/half_default_margin"
                    android:gravity="end" />

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/containerServices"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/half_default_margin"
            android:layout_marginTop="@dimen/half_default_margin"
            android:layout_marginEnd="@dimen/half_default_margin"
            android:layout_marginBottom="@dimen/half_default_margin"
            android:orientation="vertical">

            <TextView
                android:id="@+id/servicesLabelTextView"
                style="@style/mapMarkerInfoItemLabelTextStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/half_default_margin"
                android:layout_marginEnd="@dimen/half_default_margin"
                android:text="@string/services_colon" />

            <LinearLayout
                android:id="@+id/gazStationsServicesContainer"
                android:layout_width="match_parent"
                android:layout_height="64dp"
                android:gravity="start|center"
                android:orientation="horizontal">

                <com.yarolegovich.discretescrollview.DiscreteScrollView
                    android:id="@+id/gazStationServiceDiscreteScrollView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:dsv_orientation="horizontal" />

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</layout>

and when I click on market then my custom widget not show. Here result:

enter image description here

How to&Answers:

You cannot float custom view directly on the markers, you can achieve by using custom markers (location icon with above your custom layout both included) and use this layout on the map and write onclick listeners to show and hide